Mapping Bag in Collection Mapping (using xml file)

If our persistent class has List object, we can map List by list or bag component in the mapping record. The file is much the same as List however it doesn't require list component.

Here, we are utilizing the scenario of Forum where one inquiry has various answers.

Mapping Bag in hibernate

Let's see how we can implement the bag in the mapping file:

  1. <class name="com.javacodegeeks.Question" table="q100">  
  2.        ...        
  3.           <bag name="answers" table="ans100">  
  4.           <key column="qid"></key>  
  5.           <element column="answer" type="string"></element>  
  6.           </bag>  
  7.             
  8.        ...  
  9. </class>  

Example of mapping file in collection mapping

In this precedent, we are going to see full case of collection mapping by bag. This is the case of file on the off chance that it stores esteem not substance reference that is for what reason are going to utilize element rather than one-to-many. On the off chance that you have seen the case of mapping show, it is same in all cases rather mapping record where we are utilizing file instead than list.

1) Create the Persistent class

This persistent class defines properties of the class including List.

  1. bagage com.javacodegeeks;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Question {  
  6. private int id;  
  7. private String qname;  
  8. private List<String> answers;  
  9.   
  10. //getters and setters  
  11.   
  12. }  

2) Create the Mapping file for the persistent class

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  5.   
  6. <hibernate-mapping>  
  7.  <class name="com.javacodegeeks.Question" table="q101">  
  8.    <id name="id">  
  9.      <generator class="increment"></generator>  
  10.    </id>  
  11.    <property name="qname"></property>  
  12.             
  13.    <bag name="answers" table="ans101">  
  14.      <key column="qid"></key>  
  15.      <element column="answer" type="string"></element>  
  16.    </bag>  
  17.             
  18.  </class>  
  19.             
  20. </hibernate-mapping>  

3) Create the configuration file

This file contains information about the database and mapping file.

  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">  
  5.   
  6. <!-- Generated by MyEclipse Hibernate Tools.                   -->  
  7. <hibernate-configuration>  
  8.   
  9.     <session-factory>  
  10.         <property name="hbm2ddl.auto">update</property>  
  11.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  12.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
  13.         <property name="connection.username">system</property>  
  14.         <property name="connection.password">jtp</property>  
  15.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  16.     <mapping resource="question.hbm.xml"/>  
  17.     </session-factory>  
  18.   
  19. </hibernate-configuration>  






javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition


python