Mapping Bag in Collection Mapping (using xml file)

If our tireless class has List object, we can outline List by rundown or pack component in the mapping record. The sack is much the same as List however it doesn't require list component.

Here, we are utilizing the situation 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.javatpoint.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 sack in gathering mapping

In this precedent, we are going to see full case of gathering mapping by pack. This is the case of sack 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 sack rather than rundown.

1) Create the Persistent class

This persistent class defines properties of the class including List.

  1. package com.javatpoint;  
  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






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