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>

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

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Microsoft Azure Architect Technologies(AZ-300) | ITGuru
    Microsoft Azure Architect Technologies(AZ-300)
  • Azure is a great Microsoft Cloud Computing platform in providing various cloud services through online. ITGuru Certified Azure Architect certification course gives you the practical knowledge on Azure Cloud platform through real-world use cases from live experts

  • ServiceNow Admin Training | ITGuru
    ServiceNow Admin Training
  • Getting knowledge of cloud platforms like ServiceNow is essential in today’s world for the smooth running of projects in cloud platform. Turn your dream to the reality of becoming the Certified ServiceNow Administrator through ServiceNow Administration online certification Course with practical examples by live industry experts through online at ITGuru with real-world use cases.

  • Workday Financials Training | ITGuru
    Workday Financials Training
  • knowing the basics on any platform like Workday is not enough to sustain the IT industry. Hence it is essential to go beyond on Workday basics like Workday Financials training which lets you know the application of Financials management in real -world use cases from ITGuru Live Experts in a practical way.

  • Workday Training
  • An organization is considered as the best one when it offers the best benefits to the employee. Moreover, the greater the employee benefits, the greater the contribution to the organization. ITGuru let you know the practical workday Human Resource Management(HRM) features with live examples by experts

  • Python Online Course | ITGuru
    Python Online Course
  • Turn your dream into reality by ITGuru live experts with real-world use cases through practical knowledge on python online course and become the certified associate in python programming and become a master in python programming

  • Python Programming | ITGuru
    Python Programming
  • Python is the trending programming language in the IT industry. Mastering in python programming gives you more value among the people in the IT industry. Hence start today to learn python programming online by live experts with real-time uses cases at ITGuru


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

Call Now Button