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

</id>

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

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

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

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

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

</list>

</class>

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>

<key

column=”columnname”

on-delete=”noaction|cascade”

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

</list>

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

 FetchData.java:

 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<Question> list=query.list();

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

while(itr.hasNext()){

Question q=itr.next();

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

//printing answers

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

Iterator<Answer> 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″>

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

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

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

</set>

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

Drop Us A Query

Trending Courses
  • Python and Django Online 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.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


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