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" /> </list> </class> <class name="roseindia.Story" table="story"> <id name="id" type="int" column="id"> <generator class="increment" /> </id> <property name="info" type="java.lang.String" column="info" length="255" /> <many-to-one name="group" class="roseindia.Group" column="parent_id" /> </class></
hibernate-mapping>
The following mapping tag defines the one-to-many mapping between Group and Story entities.
<list name="stories" cascade="all"> <key column="parent_id" /> <index column="idx" /> <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.
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:
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:
The create ArrayList object set and add many Story objects:
// Array list to hold storiesArrayList list =
new ArrayList(); // Create StoriesStory javaHistory =
new Story();javaHistory.setInfo(
"Java History");list.add(javaHistory);
// Create StoriesStory javaPlatform =
new Story();javaPlatform.setInfo(
"Java Platforms");list.add(javaPlatform);
// Create StoriesStory javaNews =
new Story();javaNews.setInfo(
"Java News");list.add(javaNews);
// Create StoriesStory jeeNews =
new Story();jeeNews.setInfo(
"JEE News");list.add(jeeNews);
Finally add the book set to the group object and save the group object:
// Add the list to Group objectgroup.setStories(list);
// Save groupsession.save(group);
Here is output of the program when executed in Eclipse IDE.
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 (?)
Hibernate: select max(id) from story
Group id:2
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 (?, ?, ?)
Hibernate: insert into story (info, parent_id, id) values (?, ?, ?)
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=?
Hibernate: update story set parent_id=?, idx=? where id=?
In this section we learned about the one-to-many relationships in Hibernate.
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.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Hibernate One-to-many Relationships View All Comments
Post your Comment