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
public class Exam { String subjects; int id; List<Results> results; public String getSubjects() { return subjects; } public void setSubjects(String subjects) { this.subjects = subjects; } public int getId() { return id; } public void setId(int id) { this.id = id; } public List<Results> getResults() { return results; } public void setResults(List<Results> results) { this.results = results; } }
Results.java
public class Results { String mark; int id; public int getId() { return id; } public void setId(int id) { this.id = id; } String sname; public String getMark() { return mark; } public void setMark(String mark) { this.mark = mark; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } }
Right click source packages –>New–>Hibernate Mapping Wizard
hibernate.hbm.xml
<hibernate-mapping> <class name="pojo.Exam" table="exam"> <id name="id"> <generator class="increment"></generator> </id> <property name="subjects" type="string"> <column name="Subjects"/> </property> <bag name="results" cascade="all"> <key column="StudentID"></key> <one-to-many class="pojo.Results"></one-to-many> </list> </class> <class name="pojo.Results" table="result"> <id name="id"> <generator class="increment"/> </id> <property name="sname" type="string"> <column name="StudentName"/> </property> <property name="mark" type="string"> <column name="Marks"/> </property> </class> </hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibercrud</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <mapping resource="hibernate.hbm.xml"/> </session-factory> </hibernate-configuration>
save.java
public class save { public static void main(String args[]) { Transaction trans=null; Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession(); trans=session.beginTransaction(); Exam exam1=new Exam(); Exam exam2=new Exam(); Results results1=new Results(); Results results3=new Results(); exam1.setSubjects("JSP"); exam2.setSubjects("Hibernate"); List l1=new ArrayList(); List l2=new ArrayList(); results1.setSname("Alpha"); results1.setMark("92"); results3.setSname("Delta"); results3.setMark("89"); l1.add(results1); l2.add(results3); exam1.setResults(l1); exam2.setResults(l2); session.save(exam1); session.save(exam2); trans.commit(); } }
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 |