0% found this document useful (0 votes)
11 views

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)
11 views

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
You are on page 1/ 11

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 = GenerationType.IDENTITY)
private int cid; //P.K

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


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

www.jtcindia.org 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@cc.co 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

www.jtcindia.org 17 Hibernate5.4 Notes


Lab 3: Collection Mapping Example

Lab3: Files Required:


1. Lab3A.java 2. Lab3B.java
3. Lab3C.java 4. Lab3D.java
5. Student.java 6. HibernateUtil.java*

1) Lab3A.java
package com.jtcindia.hibernate;

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

Transaction tx=null;
try {

SessionFactorysessionFactory= HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();

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

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


skills.add("Java8");
skills.add("JDBC");
skills.add("Servlets");
skills.add("JSP");

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


marks.add(80);
marks.add(85);
marks.add(88);
marks.add(92);

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


interviews.add("InfoSys");
interviews.add("TCS");
interviews.add("Google");
interviews.add("Amazon");

www.jtcindia.org 18 Hibernate5.4 Notes


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

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


stu.setCourses(courses);
stu.setSkills(skills);
stu.setMarks(marks);
stu.setInterviews(interviews);
stu.setRefs(refs);

session.save(stu);//

tx.commit();
session.close();
}
catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}

2)Lab3B.java
package com.jtcindia.hibernate;

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

Transaction tx=null;
try {
SessionFactorysessionFactory= HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();

Student stu = session.load(Student.class,1);


System.out.println(stu.getSname()+"\t"+stu.getPhone());

www.jtcindia.org 19 Hibernate5.4 Notes


String cous[] = stu.getCourses();
for(String mycou:cous) {
System.out.println(mycou);
}

List<String> myskills = stu.getSkills();


System.out.println(myskills);

List<Integer> mymarks = stu.getMarks();


System.out.println(mymarks);

Set<String> myinterviews = stu.getInterviews();


System.out.println(myinterviews);

Map<String,Integer> myrefs= stu.getRefs();


System.out.println(myrefs);

tx.commit();
session.close();
}
catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}

3)Lab3C.java
package com.jtcindia.hibernate;

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

Transaction tx=null;
try {
SessionFactorysessionFactory= HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction(); //***

www.jtcindia.org 20 Hibernate5.4 Notes


Student stu = session.load(Student.class,1);
stu.setEmail("som@jtc");
stu.setPhone(12345); //Update

//Updating Marks
List<Integer> marks = stu.getMarks();
marks.add(95);

stu.setMarks(marks); //Update

//Update Interviews
Set<String> interviews = stu.getInterviews();
interviews.add("facebook");

stu.setInterviews(interviews); //Update

//Update REfs
Map<String,Integer> refs = stu.getRefs();
refs.put("EE", 55);

stu.setRefs(refs); //Update

tx.commit();
session.close();
}
catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}

4)Lab3D.java
package com.jtcindia.hibernate;

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

Transaction tx=null;
try {
SessionFactorysessionFactory= HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();

www.jtcindia.org 21 Hibernate5.4 Notes


tx = session.beginTransaction();

//Student stu = session.load(Student.class,1);


Student stu = session.get(Student.class,1);
if(stu!=null)
session.delete(stu);
else {
System.out.println("OOOPS - No Student Found");
}
tx.commit();
session.close();
}
catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}

5)Customer.java
package com.jtcindia.hibernate;

import java.util.*;
import javax.persistence.*;
/*
* @Author : Somprakshrai
* @Company: JtcIndia
* */
@Entity
@Table(name = "mystudents")
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "sid")
private int sid;

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

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

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

www.jtcindia.org 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) {
this.sname = sname;
this.email = email;
this.phone = phone;
}
public Student(int sid, String sname, String email, int phone) {
this.sid = sid;
this.sname = sname;
this.email = email;
this.phone = phone;
}
//Setters and Getters
@Override
public String toString() {
return "Customer [" + sid + ", " + sname + ", " + email + ", " + phone + "]";
}
}

www.jtcindia.org 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:

www.jtcindia.org 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=session.load(Student.class, 1);
Ans:

Q36) What Happens when I execute the following Code?


Student stu=session.load(Student.class, 1);
System.out.println(stu.getSid());
Ans:

Q37) What Happens when I execute the following Code?


Student stu=session.load(Student.class, 1);
System.out.println(stu.getEmail());
Ans:

www.jtcindia.org 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:

www.jtcindia.org 26 Hibernate5.4 Study


Guide

You might also like