0% found this document useful (0 votes)
29 views11 pages

Module 2 - Collection Mapping

Uploaded by

yaduvanshid44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

Module 2 - Collection Mapping

Uploaded by

yaduvanshid44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Hibernate Mappings

 Hibernate supports various mapping styles:


1) Simple Mapping
2) Collection Mapping
3) Inheritance Mapping
a. Table per sub class mapping
b. Table per class mapping
c. Table per concrete class mapping
4) Association Mapping
a. One - to - One Mapping
b. One - to - Many Mapping
c. Many- to - Many Mapping
5) Other mappings.

Simple Mapping

 When you map your Hibernate Entity Class with simple data types like String, Primitives,
Wrappers, Date etc with the corresponding table columns then it is called as Simple
Mapping.
Ex:
public class Customer {
private int cid;
private String cname;
private String email;

}
Annotation Required for Simple Mapping:
1) You need to use following Annotations for Persistence class
@Entity
@Table(name = "customers")
class Customer { … }

2) You need to use following Annotations for primary key field


@Id
@Column(name="cid")
@GeneratedValue(strategy = [Link])
private int cid; //P.K

3) You need to use following Annotations for other simple fields


@Column(name="cname")
private String came;

[Link] 16 Hibernate5.4 Notes


Collection Mapping

 Hibernate Entity Class can have fields of collection data types like array, List, Set, Map. You
need to persistent the Collections in a separate table always.
 You can use Collection mapping to map the Collection Types with Child Tables.

Collection Mapping Example


A) Persistence Class
class Student{
int sid; // P.K
String sname;
String email;
String phone;
String [] courses;
List<String> skills;
List<Integer> marks;
Set<String> interviews;
Map<String,Long> refs;

}
B) Tables
myskills
mystudents sid skillName expertiseOrder
sid sname email phone 101 Java 0
101 Som som@[Link] 12345 101 JDBC 1
m

mycourses myrefs
mysid courseName learningOrder mysid refName refPhone
101 Java 0 101 aaa 1111
101 JDBC 1 101 Bbb 2222
101 JSP 2 101 ccc 3333
101 Servlets 3
mymarks myinterviews
mysid marksObtained mysid companyName
101 50 101 Capgemini
101 60 101 TCS
101 70 101 Infosys

[Link] 17 Hibernate5.4 Notes


Lab 3: Collection Mapping Example

Lab3: Files Required:


1. [Link] 2. [Link]
3. [Link] 4. [Link]
5. [Link] 6. [Link]*

1) [Link]
package [Link];

import [Link].*;
import [Link].*;
/*
* @Author : Somprakshrai
* @Company: JtcIndia
* */
public class Lab3A {
public static void main(String[] args) {

Transaction tx=null;
try {

SessionFactorysessionFactory= [Link]();
Session session = [Link]();
tx = [Link]();

String courses[] = {"Hibernate","Spring","Spring Boot","Angular"};

List<String> skills = new ArrayList<>();


[Link]("Java8");
[Link]("JDBC");
[Link]("Servlets");
[Link]("JSP");

List<Integer> marks = new ArrayList<>();


[Link](80);
[Link](85);
[Link](88);
[Link](92);

Set<String> interviews = new HashSet<>();


[Link]("InfoSys");
[Link]("TCS");
[Link]("Google");
[Link]("Amazon");

[Link] 18 Hibernate5.4 Notes


Map<String,Integer> refs = new HashMap<>();
[Link]("AA", 11);
[Link]("BB",22);
[Link]("CC", 33);
[Link]("DD", 44);

Student stu = new Student("Som","Som@jtc",12345);


[Link](courses);
[Link](skills);
[Link](marks);
[Link](interviews);
[Link](refs);

[Link](stu);//

[Link]();
[Link]();
}
catch(Exception ex) {
[Link]();
[Link]();
}
}
}

2)[Link]
package [Link];

import [Link].*;
import [Link].*;
/*
* @Author : Somprakshrai
* @Company: JtcIndia
* */
public class Lab3B {
public static void main(String[] args) {

Transaction tx=null;
try {
SessionFactorysessionFactory= [Link]();
Session session = [Link]();
tx = [Link]();

Student stu = [Link]([Link],1);


[Link]([Link]()+"\t"+[Link]());

[Link] 19 Hibernate5.4 Notes


String cous[] = [Link]();
for(String mycou:cous) {
[Link](mycou);
}

List<String> myskills = [Link]();


[Link](myskills);

List<Integer> mymarks = [Link]();


[Link](mymarks);

Set<String> myinterviews = [Link]();


[Link](myinterviews);

Map<String,Integer> myrefs= [Link]();


[Link](myrefs);

[Link]();
[Link]();
}
catch(Exception ex) {
[Link]();
[Link]();
}
}
}

3)[Link]
package [Link];

import [Link].*;
import [Link].*;
/*
* @Author : Somprakshrai
* @Company: JtcIndia
* */
public class Lab3C {
public static void main(String[] args) {

Transaction tx=null;
try {
SessionFactorysessionFactory= [Link]();
Session session = [Link]();
tx = [Link](); //***

[Link] 20 Hibernate5.4 Notes


Student stu = [Link]([Link],1);
[Link]("som@jtc");
[Link](12345); //Update

//Updating Marks
List<Integer> marks = [Link]();
[Link](95);

[Link](marks); //Update

//Update Interviews
Set<String> interviews = [Link]();
[Link]("facebook");

[Link](interviews); //Update

//Update REfs
Map<String,Integer> refs = [Link]();
[Link]("EE", 55);

[Link](refs); //Update

[Link]();
[Link]();
}
catch(Exception ex) {
[Link]();
[Link]();
}
}
}

4)[Link]
package [Link];

import [Link].*;
/*
* @Author : Somprakshrai
* @Company: JtcIndia
* */
public class Lab3D {
public static void main(String[] args) {

Transaction tx=null;
try {
SessionFactorysessionFactory= [Link]();
Session session = [Link]();

[Link] 21 Hibernate5.4 Notes


tx = [Link]();

//Student stu = [Link]([Link],1);


Student stu = [Link]([Link],1);
if(stu!=null)
[Link](stu);
else {
[Link]("OOOPS - No Student Found");
}
[Link]();
[Link]();
}
catch(Exception ex) {
[Link]();
[Link]();
}
}
}

5)[Link]
package [Link];

import [Link].*;
import [Link].*;
/*
* @Author : Somprakshrai
* @Company: JtcIndia
* */
@Entity
@Table(name = "mystudents")
public class Student {

@Id
@GeneratedValue(strategy = [Link])
@Column(name = "sid")
private int sid;

@Column(name = "sname")
private String sname;

@Column(name = "email")
private String email;

@Column(name = "phone")
private int phone;

[Link] 22 Hibernate5.4 Notes


@ElementCollection
@CollectionTable(name = "mycourses", joinColumns = @JoinColumn(name = "mysid"))
@OrderColumn(name = "learningOrder")
@Column(name = "courseName")
private String[] courses;

@ElementCollection
@CollectionTable(name = "myskills", joinColumns = @JoinColumn(name = "mysid"))
@OrderColumn(name = "experstiseOrder")
@Column(name = "skillName")
private List<String> skills;

@ElementCollection
@CollectionTable(name = "mymarks", joinColumns = @JoinColumn(name = "mysid"))
@Column(name = "marks")
private List<Integer> marks;

@ElementCollection
@CollectionTable(name = "myinterviews", joinColumns = @JoinColumn(name = "mysid"))
@Column(name = "companyName")
private Set<String> interviews;

@ElementCollection
@CollectionTable(name = "myrefs", joinColumns = @JoinColumn(name = "mysid"))
@MapKeyColumn(name = "refName")
@Column(name = "refPhone")
private Map<String, Integer> refs;

public Student() { }
public Student(String sname, String email, int phone) {
[Link] = sname;
[Link] = email;
[Link] = phone;
}
public Student(int sid, String sname, String email, int phone) {
[Link] = sid;
[Link] = sname;
[Link] = email;
[Link] = phone;
}
//Setters and Getters
@Override
public String toString() {
return "Customer [" + sid + ", " + sname + ", " + email + ", " + phone + "]";
}
}

[Link] 23 Hibernate5.4 Notes


Interview Questions:
Q19) What are the Loading Strategies supported by Hibernate?
Ans:

Q20) What is the Aggressive Loading?


Ans:

Q21) What is the Lazy Loading?


Ans:

Q22) What are the Fetching Strategies supported by Hibernate?


Ans:

Q23) When to use Collection Mapping?


Ans:

Q24) How to map the Array type Fields of Entity Class?


Ans:

Q25) What is the default Loading Strategy followed for Arrays?


Ans:

Q26) What is the default Fetching Strategy followed for Arrays?


Ans:

Q27) How to change the default Loading Strategy followed for Arrays?
Ans:

Q28) How to change the default Fetching Strategy followed for Arrays?
Ans:

[Link] 24 Hibernate5.4 Notes


Q29) How to map the List/Set/Map type Fields of Entity Class?
Ans:

Q30) What is the default Loading Strategy followed for List/Set/Map?


Ans:

Q31) What is the default Fetching Strategy followed for List/Set/Map?


Ans:

Q32) How to change the default Loading Strategy followed for List/Set/Map?
Ans:

Q33) How to change the default Fetching Strategy followed for List/Set/Map?
Ans:

Q34) What is the default Loading Strategy followed for Entity Class?
Ans:

Q35) What Happens when I execute the following Code?


Student stu=[Link]([Link], 1);
Ans:

Q36) What Happens when I execute the following Code?


Student stu=[Link]([Link], 1);
[Link]([Link]());
Ans:

Q37) What Happens when I execute the following Code?


Student stu=[Link]([Link], 1);
[Link]([Link]());
Ans:

[Link] 25 Hibernate5.4 Notes


Q38) What is Cascading?
Ans:

Q39) What are the Cascading Types supported in Hibernate?


Ans:

Q40) What Cascade Delete?


Ans:

Q41) What Happens when Given P.K value is not found when I call get() method?
Ans:

Q42) What Happens when Given P.K value is not found when I call load() method?
Ans:

Q43) What is the difference between get() and load() ?


Ans:

Q44) Can I run multiple Txs in One Session?


Ans:

[Link] 26 Hibernate5.4 Study


Guide

You might also like