Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Collection Mapping
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 answers;//List can be of any type//getters and setters}

JAVA Tutorial Video

Mapping collection in mapping file:

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

<class name="com.javaspot.Question" table="q100">class="increment">class>There are three subelements utilized as a part of the rundown:

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

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

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 or 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.

null="true" >null="true|false"property-ref="propertyName"update="true|false"unique="true|false"/>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 .

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 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">

class>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 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 objectclass="com.javaspot.Answer"/>  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").buildSessionFactory().openSession();Query query=session.createQuery("from Question");List list=query.list();Iterator itr=list.iterator();while(itr.hasNext()){Question q=itr.next();System.out.println("Question Name: "+q.getQname());//printing answersList list2=q.getAnswers();Iterator itr2=list2.iterator();while(itr2.hasNext()){System.out.println(itr2.next());}}session.close();System.out.println("success");}}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">......class>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.