jump to navigation

Time to Hibernate July 27, 2006

Posted by jbwan in Technology.
trackback

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

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: