Hibernate One-to-many Relationships
Hibernate One-to-many Relationships - One to many example code in Hibernate using the xml file as metadata.
Here you will learn one-to-many relationships developed using the hmb.xml file. The one-to-many tag is used to define the relationships.
One-to-Many Relationships
In case of one-to-many relationships, each row in table A linked to multiple rows in table B. Here we will implement one-to-many relationship between two entries Group and Story. There exists multiple stories under a group.
We are using two tables grouptable and story and java entities Group and Story maps respectively. Here is the ER diagram

Java Persistence Objects:
The code for Group.java is given below:
|
The code for Story.java is given below:
package roseindia; |
Mapping xml file (Group.hbm.xml):
This file contains the mapping for both the entities Stroy and Group which maps with the tables gropuptable and story respectively.
<?
xml version="1.0"?><!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<
hibernate-mapping> <class name="roseindia.Group" table="gropuptable"> <id name="id" type="int" column="id"> <generator class="native" /> </id> <property name="name" type="java.lang.String" column="name" length="50" /> <list name="stories" cascade="all"> <key column="parent_id" /> <index column="idx" /> <one-to-many class="roseindia.Story" /> 0 </list> </class>1
<class name="roseindia.Story" table="story"> <id name="id" type="int" column="id"> <generator class="increment" /> 2 </id> <property name="info" type="java.lang.String" column="info" length="255" /> 3 <many-to-one name="group" class="roseindia.Group" column="parent_id" /> </class></
hibernate-mapping> 4
The following mapping tag defines the one-to-many mapping between Group and Story entities. 5 <list name="stories" cascade="all">
<key column="parent_id" /> <index column="idx" /> 6 <one-to-many class="roseindia.Story" /> </list>In Group.java we have stories variable of the list type, so we have used the <list../> tag here. The <one-to-many ../> specifies the one to many relationships to Story entity. 7
Running the Program:
To run and test the program you have to execute the OneToManyRelation.java. Here is the full code of OneToManyRelation.java file:
8
package roseindia;
|
The above code is self explanatory, we are creating the objects of Group:
// Group Object
Group group = new Group("Java Group 2");
Then set the author name: 9
The create ArrayList object set and add many Story objects: 0 // Array list to hold stories
ArrayList list =
new ArrayList();1
// Create StoriesStory javaHistory =
new Story();javaHistory.setInfo(
"Java History"); 2list.add(javaHistory);
// Create Stories 3Story javaPlatform =
new Story();javaPlatform.setInfo(
"Java Platforms");list.add(javaPlatform); 4
// Create StoriesStory javaNews =
new Story(); 5javaNews.setInfo(
"Java News");list.add(javaNews);
6
// Create StoriesStory jeeNews =
new Story();jeeNews.setInfo(
"JEE News"); 7list.add(jeeNews);
8
Finally add the book set to the group object and save the group object:
// Add the list to Group objectgroup.setStories(list); 9
// Save groupsession.save(group); 0
Here
is output of the program when executed in Eclipse IDE. 1
log4j:WARN No appenders could be found for logger
(org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Hibernate: insert into gropuptable (name) values (?) 2
Hibernate: select max(id) from story Group id:2 Hibernate: insert into story (info, parent_id, id)
values (?, ?, ?) 3
Hibernate: insert into story (info, parent_id, id)
values (?, ?, ?) Hibernate: insert into story (info, parent_id, id)
values (?, ?, ?) Hibernate: insert into story (info, parent_id, id)
values (?, ?, ?) 4
Hibernate: update story set parent_id=?, idx=? where
id=? Hibernate: update story set parent_id=?, idx=? where
id=? Hibernate: update story set parent_id=?, idx=? where
id=? 5
Hibernate: update story set parent_id=?, idx=? where
id=?
In this section we learned about the one-to-many relationships in Hibernate. 6
In next section we will learn how to create and run the many-to-many mapping in Hibernate. Many to many relationships is interesting as one extra table is needed to persist the relationship.


