jump to navigation

Time to Hibernate July 27, 2006

Posted by jbwan in Technology.
add a comment

This week I finally came to grips with a large amount of Hibernate logic. Still by no means an expert on this new-fangled technology but definitely making progress. For those not in the know, Hibernate is a lightweight framework for database connectivity in Java, replacing the old JDBC type connectivity that gave us all so much heartache. It is object-relational (OR) which means that mapping files create the logic for POJOs to be serialised to the database tables without any real overhead on the developer. Some would say it just works!

Basic Hibernate examples are easy enough to follow but when you need to step up a gear the tutorials that I found are a little confusing and not very descriptive of what is happening or exactly what you are doing. For example, today I wanted to create a join between two tables and return the output. Typically in standard SQL life I would have written a simple SQL query and returned the result set accessing each field as required. However, OR doesn’t work like that as every query result must be castable to an object in order to access the goodies.

The following class represents a simple Locker that stores a user id and associated content item.

public class Locker implements Serializable
{
private static final long serialVersionUID = -3422590115566066246L;
private long id;
private long user_id;
private Content content;

public Content getContent()
{
return content;
}

public void setContent(Content content)
{
this.content = content;
}

public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}

public long getUser_id()
{
return user_id;
}

public void setUser_id(long user_id)
{
this.user_id = user_id;
}

public Locker() {  }
}

The following class represents a content item:

public class Content implements Serializable
{
private static final long serialVersionUID = -8407993048001055808L;
private long id;
private String content_description;
private double price;

public Content() { }

public String getContent_description()
{
return content_description;
}

public void setContent_description(String content_description)
{
this.content_description = content_description;
}

public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}

public double getPrice()
{
return price;
}

public void setPrice(double price)
{
this.price = price;
}
}

This is the hibernate mapping file for the content class:

<hibernate-mapping>
<class name="org.hibernate.Content" table="CONTENT">
 <id name="id" column="CONTENT_ID">
     <generator class="native">
 </id>

 <property name="content_description">
 <property name="price">
</class>
</hibernate-mapping>

This is the magic file 🙂 for mapping the locker class to the content item:

<hibernate-mapping>
<class name="org.hibernate.Locker" table="LOCKER">

 <id name="id" column="LOCKER_ID">
     <generator class="native">
 </id>
 <property name="user_id">
 <many-to-one name="content" class="org.hibernate.Content" column="CONTENT_ID">
</class>

</hibernate-mapping>

Note the many-to-one element that links the two objects via their relational table keys, in this case the content_id foreign key for the locker table. This is all that is required to be able to select * from locker, content where locker.user_id=’joe’ and locker.content_id = content.id, and get back proper POJOs for accessing the content from both tables. The locker table information plus the referenced information from the content table. Hibernate rocks!

Advertisements

British Nuclear Plan July 12, 2006

Posted by jbwan in General, Politics.
comments closed

The British government published it energy review yesterday and sadly it has included the building of nuclear power plants to meet their energy needs. I sincerely hope that all Irish people will rise up in protest against this given the safety record maintained by Sellafield’s reprocessing plant. It’s clear that the level of care and committment necessary to run a most dangerous energy source is not currently being practiced. source: Irish Times

Note: Irish Times article may require login

12 people, 48 hours – a death every 240 minutes July 11, 2006

Posted by jbwan in General.
comments closed

This week beginning 10 July 2006 has been particularly bad for Irish families. Road deaths appear to be dramatically rising, although the way the media portrays it you would think it was 100’s more than last year instead of the real figure of 22 compared with this time in 2005.

Now I’m not making light of a very serious situation, far from it: Any death on our roads is certainly a tragic event. The media however, must realise that they do not control this, that they have no command over the actions of drivers. If anything, it could even be argued that listening to radio stations while driving is a distraction that may cause accidents, placing the media and its offering of light-hearted escapism directly at the cause of accidents. That of course is hypothetical and I am not suggesting that this is the case.

While the media may clamber on from their pockets of non-news with flavour of the month stories, just like that outbreak of Ebola in Africa about 12 years ago that was going to wipe us all out (I think the story lasted 2 weeks and I have heard nothing since), they often fail to address the real issue. Why are people dying on our roads? The answer is non-trivial but not entirely incomprehensible.

People do not die because they are young, they do not die because they are learner drivers. Statistics can be gathered for any argument and as such great attention should be paid to sample frames and other factors used in these condemning reports. A quick example: If a young person dies on the road, it doesn’t mean that they caused the accident or even that they were in a car. It’s all about the resolution of the figure presented.

The main reason that people die on our roads is down to aggression. Aggressive driving is not common amongst those starting off in life, in fact it is more often caused by experienced drivers who cannot bear to have a slow driver ahead of them doing perhaps 10kmh under the limit. Another problem associated with experienced drivers is a complete lack of vigilance and road ethics. Has anyone ever driven in Dublin lately? I swear every car sold there was sold without indicators and incredibly bad tracking; nobody ever stays in lane or indicates to say that they are changing lanes. Remember before you sat your driving test, remember how rigidly you stuck to the left side of the road and never in your life would you ever go near the middle of two lanes on a two-lane roundabout. Yet so many times I have had to brake for older drivers who nearly run me off the roundabout by crossing over to this middle section.

It has also been said that many accidents happen at night or the early hours of the morning. Why do people need to be out driving in the early hours of the morning? Social activity? Perhaps. Working? Probably a lot of cases are attributable to people travelling to and from work. In many cases our roads are full of people who are exhausted, having worked long hours for little gain, returning home to collect the kids they see for 2 hours a day, pandering to the need for money above life. These days, people are practically forced to own cars by their employers – it was never the case for the generation that built the mighty Ireland so many years ago. Yet more and more people are flocking to the roads at increasingly different hours of the day and night, resulting in a constant stream of traffic all day long.

How do we stop road accidents? We don’t! No matter what measures are put in place, accidents will happen. People will be careless and sometimes things just cannot be avoided. As every media and insurance company instrument likes to say, there are so many accidents caused by young drivers. However, they often neglect to say that this figure is a percentage of many road accidents, perhaps not even a majority percentage. The point here is that out of every set of data there will always be a percentage, perhaps our focal set of data is cars on the road, then we conclude that a percentage will crash. It is therefore not unreasonable to assume that if the number of cars on the roads increases then so too may the number of crashes; it’s all percentage linked, the percentage doesn’t change but the absolute value does.

How do we stop road accidents? We don’t! We simply control the number of potential road accidents by removing the need for people to drive 24/7.

P2P July 5, 2006

Posted by jbwan in Technology.
add a comment

It’s simple to use, simple to get your hands on and simple to share but P2P doesn’t appear that simple when you want to implement it. Don’t get me wrong, the concept is not totally alien to me: Sure I set up a client and it makes itself known to others, possibly advertising a hash bucket of wares that it possesses. Other clients can search this mesh of public hash buckets for things that they want and retrieve a link to download. That’s it in a nutshell? Sadly not, the real deal embodies far more – I like this description on Wikipedia. Amongst the most challenging of tasks is how to write a sophisticated and scalable overlay network for the P2P architecture. It’s non-trivial to say the least. I remember a colleague of mine sending me a link many years ago to an article on why Gnutella Can’t Scale. The calculations presented in this article are all simple math and some of the figures are quite frankly astounding. The article openly admits that many assumptions are made throughout but the case presented is no less tarnished for them. P2P networks truly are a bandwidth killer if not structured and controlled in some thoughtful fashion.