OneToMany in Hibernate using Annotations

OneToMany in Hibernate using Annotations
Here we are going to implement OneToMany mapping in hibernate using Annotations.

We are going to have 2 entities Address and Employee. Here we are assuming that an employee can have more than one address. When we enter the employee details and his addresses (Here we are going to enter 2 addresses for one employee) a 3rd table creating which brings the relationship between the both Employee and Address table.

op1

Once we go through the example, the concept will be clear.

Structure:
Structure

hibernate.cfg.xml

NewHibernateUtil.java

Address.java

Employee.java

Execution.java

Output:
op1

We can see 3 tables has been created, Employee_Mapping, Address_Mapping and Joined_Table. Here the relationship between the Employee_Mapping and Address_Mapping is pointed out in JoinedTable

op4

we can see that employee_ID – 3, that employee has 2 address that are mapped in address table (5,6)

This is know an OneToMany Mapping

 

 

 

 

Read More

one-to-many mappings by Set in Hibernate

one-to-many mappings by Set in Hibernate
one-to-many mapping by Set in Hibernate. This code is tested using NetBeans.

Set is a collection that don’t allow duplicates and does not maintain any order

NetBeans
1) File–>New–>Java–>Web Application–> Next
2) Give application a name
3) Next–>Next–> Choose Hibernate Framework and select the database to connect

Inside Source Packages create a folder named “pojo”
Inside pojo create 3 Java Class files
1) Exam.java
2) Results.java
3)save.java

Exam.java

Results.java

Right click source packages –>New–>Hibernate Mapping Wizard
hibernate.hbm.xml

hibernate.cfg.xml

save.java

Output:

Exam Table

id Subjects
1 JSP
2 Hibernate

Results Table

id StudentName Marks StudentID Type
1 Alpha 92 1 0
2 Delta 89 2 0

Read More

one-to-many mapping by Bag in Hibernate

one-to-many mapping by Bag in Hibernate
one-to-many mapping by Bag in Hibernate. This code is tested using NetBeans.

Bag is a collection of elements that can have duplicates and does not have any order

NetBeans
1) File–>New–>Java–>Web Application–> Next
2) Give application a name
3) Next–>Next–> Choose Hibernate Framework and select the database to connect

Inside Source Packages create a folder named “pojo”
Inside pojo create 3 Java Class files
1) Exam.java
2) Results.java
3)save.java

Exam.java

Results.java

Right click source packages –>New–>Hibernate Mapping Wizard
hibernate.hbm.xml

hibernate.cfg.xml

save.java

Output:

Exam Table

id Subjects
1 JSP
2 Hibernate

Results Table

id StudentName Marks StudentID Type
1 Alpha 92 1 0
2 Delta 89 2 0

Read More

one-to-many Mapping by List in Hibernate

one-to-many Mapping by List in Hibernate
one-to-many Mapping by List in Hibernate. This code is tested using NetBeans.

You can use List when you want to allow Duplicate items and to store in Ascending Order

NetBeans
1) File–>New–>Java–>Web Application–> Next
2) Give application a name
3) Next–>Next–> Choose Hibernate Framework and select the database to connect

Inside Source Packages create a folder named “pojo”
Inside pojo create 3 Java Class files
1) Exam.java
2) Results.java
3)save.java

Exam.java

Results.java

Right click source packages –>New–>Hibernate Mapping Wizard
hibernate.hbm.xml

hibernate.cfg.xml

save.java

Output:

Exam Table

id Subjects
1 JSP
2 Hibernate

Results Table

id StudentName Marks StudentID Type
1 Alpha 92 1 0
2 Delta 89 2 0

Read More