Hibernate many to one example using XML, many-to-one is the type of relationship in Hibernate. This is child to parent relationship where many children can belong to one parent.
1. Overview of Hibernate many to one relationship example
Here, I am going to establish a relationship between two different entity/domain model classes using XML for Hibernate many to one mapping relationship.
I have tested this program code using:
– JDK 7
– MySQL 5.5
– Eclipse IDE
– Hibernate 3
For library/.jar dependecy you can download source code which is mention at the last of our post hello world program in Hibernate. And for simplicity, I kept all the files under one package for example: com.mysoftkey.relationship.many2one
. At the end of this post you will learn, how to establish relationship of many-to-one in Hibenate using .hbm.xml
files.
2. What is Hibernate many-to-one Relationship
This is Hibernate many to one mapping relationship example, for simplicity I keep all the classes in one package i.e. com.mysoftkey.relationship.many2one
and then tested the code in Eclipse IDE, JDK 6, MySQL 5.5.
Here, you can say, many students can have one address. in this example many children (student entity) can have one parent ( i.e. Address entity).
In this example, you will learn how to map many-to-one relationships using Hibernate. Consider the following relationship between Student and Address entity. According to the relationship, many students can have the same address.
To create this relationship you need to have a STUDENT and ADDRESS table. The cascade option is used to cascade the required operations to the associated entity. If the cascade option is set to all then all the operations will be cascaded. For instance when you save a Student object, the associated Address object will also be saved automatically.
3. Domain model for many-to-one relationship
This is the domain model classes for mnay to one example in Hibernate.
File: com/mysoftkey/relationship/many2one/Student.java
package com.mysoftkey.relationship.many2one; import java.io.Serializable; import java.util.HashSet; import java.util.Set; public class Student implements Serializable { private long studentId; private String StudentName; private Address studentAddress; public Student() { } public Student(String StudentName, Address studentAddress) { this.StudentName = StudentName; this.studentAddress = studentAddress; } // Generate setter/gettetr methods, this is required // omitted to short and clear the code }
File: com/mysoftkey/relationship/many2one/Address.java
package com.mysoftkey.relationship.many2one; import java.io.Serializable; public class Address implements Serializable { private long addressId; private String street; private String city; private String state; private String zipcode; public Address() { } public Address(String street, String city, String state, String zipcode) { this.street = street; this.city = city; this.state = state; this.zipcode = zipcode; } // Generate setter/gettetr methods, this is required // omitted to short and clear the code}
4. mapping for many to one relationship
In this section you will learn how to establish many to one relationship in Hibernate using XML.
File: com/mysoftkey/relationship/many2one/Student.hbm.xml
File: com/mysoftkey/relationship/many2one/Address.hbm.xml
5. Hibernate Configuration for many-to-one relationship
This file is Hbirnate configuration file, and keep this in root folder of your application.
File: hibernate.cfg.xml
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/hibernate_example root root 2 org.hibernate.dialect.MySQLDialect true create
6. Run Client of Many to one relationship
File: com/mysoftkey/relationship/many2one/MainManyToOneClient.java
package com.mysoftkey.relationship.many2one; import org.hibernate.HibernateException; import org.hibernate.Transaction; import org.hibernate.classic.Session; public class ManyToOneExampleClient { /** * @param args */ /* * Hibernate Mapping Many-to-One * * In this example you will learn how to map many-to-one relationship using * Hibernate XML Mapping. Consider the following relationship between Student and Address * entity. According to the relationship many students can have the same * address. * */ public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); Address address = new Address("Dwarka", "New Delhi", "ND", "110045"); // By using cascade=all option the address need not be saved explicitly // when the student object is persisted the address // will be automatically saved. // session.save(address); Student student1 = new Student("Ashish", address); Student student2 = new Student("Ranjeet", address); session.save(student1); session.save(student2); transaction.commit(); } catch (HibernateException e) { transaction.rollback(); e.printStackTrace(); } finally { session.close(); } } }
7. Output of many to one hibernate relationship
Eclipse console output:
Hibernate: select max(STUDENT_ID) from STUDENT_many2one Hibernate: select max(ADDRESS_ID) from address_many2one Hibernate: insert into address_many2one (ADDRESS_STREET, ADDRESS_CITY, ADDRESS_STATE, ADDRESS_ZIPCODE, ADDRESS_ID) values (?, ?, ?, ?, ?) Hibernate: insert into STUDENT_many2one (STUDENT_NAME, STUDENT_ADDRESS, STUDENT_ID) values (?, ?, ?) Hibernate: insert into STUDENT_many2one (STUDENT_NAME, STUDENT_ADDRESS, STUDENT_ID) values (?, ?, ?)
mySQL terminal output:
mysql> select * from address_many2one; +------------+----------------+--------------+---------------+-----------------+ | ADDRESS_ID | ADDRESS_STREET | ADDRESS_CITY | ADDRESS_STATE | ADDRESS_ZIPCODE | +------------+----------------+--------------+---------------+-----------------+ | 1 | Dwarka | New Delhi | ND | 110045 | +------------+----------------+--------------+---------------+-----------------+ 1 row in set (0.00 sec) mysql> select * from STUDENT_many2one; +------------+--------------+-----------------+ | STUDENT_ID | STUDENT_NAME | STUDENT_ADDRESS | +------------+--------------+-----------------+ | 1 | Ashish | 1 | | 2 | Ranjeet | 1 | +------------+--------------+-----------------+ 2 rows in set (0.00 sec) mysql>
8. Reference
Thanks for visiting this post for learning Hibernate many to one example relationship. You can also visit Java Hibernate ORM Tutorial Listing page for more articles on Hibernate ORM framework.
Happy Learning 🙂 for many to one example in Hibernate.
Your comments are welcome to improve this many-to-one relationship in Hibernate. Happy Learning 🙂