Module 2 - Collection Mapping
Module 2 - Collection Mapping
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 { … }
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.
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
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();
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();
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(); //***
//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();
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;
@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 + "]";
}
}
Q27) How to change the default Loading Strategy followed for Arrays?
Ans:
Q28) How to change the default Fetching Strategy followed for Arrays?
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:
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: