| More
Persisting a Class Across Tables with Hibernate
 


Persisting a Class Across Tables with Hibernate

By Jim White (Instructor and Director of Training at Intertech)

Hibernate is a very versatile framework.  Need to persist a Java object into a relational database?  Hibernate provides a way to map the associated Java class's properties to the database table(s) in just about any way you can imagine.  The idea is that the mapping framework should not constrain how you design your database tables or how you design your Java classes.

Pretty regularly, Java objects are often more fine-grained that the database tables that hold their state.  For example, you might have separate Java Customer and Address objects, but have just a single customer table that stores both customer and address data.  This "denormalization" in the database is often done for increased performance or simplicity (see Hibernate's Core documentation on something called components here).

A few weeks ago, I had a couple of students (thanks Anna and Kartik) ask if it was possible to do the opposite.  That is, is it possible to map a single Java class across multiple tables?  Given Hibernate's versatility and flexibility it is probably not unexpected to learn that it is very much possible – although a little more unusual.  Here is an example to show how this is done.

First, here at two tables (Property and Address) which will hold a single object's (RealEstate) state data.

create table address(
id integer GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
street varchar(50),
city varchar(50),
state varchar(50),
zip int
);

create table property(
id integer GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
square_footage int,
bedrooms int,
bathrooms int,
agent_name varchar(50)
);

 

Now here is the single class to be mapped to these two tables complete with Hibernate mapping annotations to provide the appropriate mapping.

package com.intertech.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.SecondaryTables;
import javax.persistence.Table;

@Entity
@Table(name="Property")
@SecondaryTables({
    @SecondaryTable(name="Address", pkJoinColumns={
        @PrimaryKeyJoinColumn(name="id", referencedColumnName="id")})})
public class RealEstate {
    @Id
    @GeneratedValue()
    private Integer id;
    @Column(table="Address")
    private String street;
    @Column(table="Address")
    private String city;
    @Column(table="Address")
    private String state;
    @Column(table="Address")
    private int zip;
    @Column(name = "square_footage")
    private int squareFootage;
    private int bedrooms;
    private int bathrooms;
    @Column(name = "agent_name")
    private String agentName;

//getters, setters and constructors removed for brevity

}

Importantly, notice the @Table and @SecondaryTables (and @SecondaryTable parameter) annotations that specify the multiple tables to store RealEstate object data.  The id's for both tables must be provided in the @SecondaryTable to indicate how rows in these tables are joined to retrieve the data for any one instance.  Notice also how the properties, like street, that are not stored in the primary table (as identified by the @Table annotation) require the @Column annotation to indicate which secondary table (in this case there is only one – the Address table) holds this state.

If you are interested in this little example code and a small little application that tests storing and retrieving RealEstate objects, you'll find it in a zip file located at Intertech's web site here.

While there are certainly exceptions, if you can dream it you should be able to map it using the Hibernate framework between your Java objects and the relational database.  If you would like to learn more about Hibernate, I would welcome your attendance in our Complete Hibernate class.  Please - come join us!


Posted by: Jim White
Posted on: 3/8/2010 at 12:22 PM
Tags: , ,
Categories: Java
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Add comment




biuquote
  • Comment
  • Preview
Loading