Collection Mapping

Click to rate this post!
[Total: 1 Average: 2]

Collection Mapping:

We can map collection of Persistent class in Hibernate. You have to pronounce the kind of gathering in Persistent class from one of the accompanying sorts.

  • java.util.List
  • java.util.Set
  • java.util.SortedSet
  • java.util.Map
  • java.util.SortedMap
  • java.util.Collection
  • or write the implementation of org.hibernate.usertype.UserCollectionType

package com.javaspot;

import java.util.List;

public class Question {

private int id;

private String qname;

private List<String> answers;

//List can be of any type

//getters and setters


JAVA Tutorial Video

Mapping collection in mapping file:

There are numerous subelements of <class> components to outline accumulation. They are <list>, <bag>, <set> and <map>. How about we perceive how we actualize the rundown for the above class.

<class name=”com.javaspot.Question” table=”q100″>

<id name=”id”>

<generator class=”increment”></generator>


<property name=”qname”></property>

<list name=”answers” table=”ans100″>

<key column=”qid”></key>

<index column=”type”></index>

<element column=”answer” type=”string”></element>



There are three subelements utilized as a part of the rundown:

<key> component is utilized to characterize the remote key in this table in view of the Question class identifier.

<index> component is utilized to distinguish the sort. Rundown and Map are recorded gathering.

<element> is utilized to characterize the component of the accumulation.

This is the mapping of accumulation if gathering stores string objects. Be that as it may, if gathering stores element reference (another class objects), we have to characterize <one-to-many> or <many-to-many> component. Presently the Persistent class will resemble.

Understanding key element:

The key component is utilized to characterize the outside key in the joined table in light of the first personality. The remote key component is nullable as a matter of course. So for non-nullable remote key, we have to determine not-invalid property, for example.

<key column=”qid” not-null=”true” ></key>









Indexed collections:

  • indexed
  • non-indexed

The List and Map accumulation are recorded while set and pack accumulations are non-filed. Here, recorded gathering implies List and Map requires an extra component <index>.

Collection Elements:

  • element
  • component-element
  • one-to-many, or
  • many-to-many

The component and part component are utilized for typical esteem, for example, string, int and so forth while one-to-numerous and many-to-many are utilized to outline reference.

Mapping List in Collection:

In the event that our steady class has List question, we can outline List effectively either by <list> component of class in mapping document or by comment.

Here, we are utilizing the situation of Forum where one inquiry has numerous answers.Multiple Mapping<class name=“com.javaspot.Question” table=”q100″>

<list name=”answers” table=”ans100″>

<key column=”qid”></key>

<index column=”type”></index>

<element column=”answer” type=”string”></element>



One to Many mapping in Hibernate

On the persistent class  off chance that the industrious class has list question that contains the substance reference, we have to utilize one-to-numerous relationship to outline list component.Here, we are utilizing the situation of Forum where one inquiry has numerous answers.

 package com.javaspot;

import java.util.List;

public class Question {

private int id;

private String qname;

private List<Answer> answers;

//getters and setters


own information in answer class.

 package com.javaspot;

public class Answer {

private int id;

private String answername;

private String postedBy;

//getters and setters



The  entity reference in a list class object

<list name=”answers” cascade=”all”>

<key column=”qid”></key>

<index column=”type”></index>

<one-to-many class=”com.javaspot.Answer”/>

</list>  How to fetch the data of List:

Here, we have utilized HQL to get every one of the records of Question class including answers. In such case, it gets the information from two tables that are utilitarian ward. Here, we are immediate printing the question of answer class, yet we have superseded the toString() technique in the Answer class returning answer name and blurb name. So it prints the appropriate response name and poster name as opposed to reference id.

 package com.javaspot;

import java.util.*;

import org.hibernate.*;

import org.hibernate.cfg.*;

public class FetchData {

public static void main(String[] args) {

Session session=new Configuration().configure(“hibernate.cfg.xml”)


Query query=session.createQuery(“from Question”);

List<Question> list=query.list();

Iterator<Question> itr=list.iterator();



System.out.println(“Question Name: “+q.getQname());

//printing answers

List<Answer> list2=q.getAnswers();

Iterator<Answer> itr2=list2.iterator();









Mapping Set in Collection

In the event that our persistent class has Set protest, we can outline Set by set component in the mapping document. The set component doesn’t require list component. The one contrast amongst List and Set is that, it stores just extraordinary esteems.

<class name=”com.javatpoint.Question” table=”q102″>

<set name=”answers” table=”ans102″>

<key column=”qid”></key>

<element column=”answer” type=”string”></element>



Set One to many by set:

In the event that the steady class has set question that contains the element reference, we have to utilize one-to-numerous relationship to outline set component. We can delineate rundown question by either set. It is non-file based and won’t permit copy components.

 Mapping Map in collection mapping using xml file:

Hibernate enables you to Map elements with the RDBMS. As we probably am aware, rundown and Map  are record based collection. If there should be an occurrence of Map, list section functions as the key and component segment acts as the esteem.

Many to Many Mapping:

We can delineate to numerous connection either utilizing set, pack, outline. Here, we will utilize delineate many-to-many mapping. In such case, three tables will be made.

 Bidirectional Association:

Bidirectional association enables us to bring points of interest of ward question from both side. In such case, we have the reference of two classes in each other.We should take a case of Employee and Address, if Employee class has-a reference of Address and Address has a reference of Employee. Moreover, you have connected coordinated or one-to-numerous relationship for the classes in mapping document too, it is known as bidirectional association.

Lazy Collection

Lazy collection stacks the kid protests on request, it is utilized to enhance execution. Since Hibernate 3.0, apathetic accumulation is empowered of course.To utilize lazy collection, you may alternatively utilize lazy=”true” trait in your gathering. It is as a matter of course genuine, so you don’t have. On the off chance that you set it to false, all the kid items will be stacked at first which will diminish execution if there should be an occurrence of enormous information.

to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

  • python training
    Python Certification Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

100% Secure Payments. All major credit & debit cards accepted.

Call Now Button