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 { … }
mystudents myskills
sid sname email phone sid skillName expertiseOrder
101 Sri [email protected] 12345 101 Java 0
101 JDBC 1
mycourses myinterviews
mysid courseName learningOrder mysid companyName
101 Java 0 101 Capgemini
101 JDBC 1 101 TCS
101 JSP 2 101 Infosys
101 Servlets 3
mymarks myrefs
mysid marksObtained mysid refName refPhone
101 50 101 aaa 1111
101 60 101 Bbb 2222
101 70 101 ccc 3333
1)Lab3A.java
package com.jlcindia.hibernate;
import java.util.*;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
Transaction tx=null;
try {
session.save(stu);//
tx.commit();
session.close();
}
catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}
2)Lab3B.java
package com.jlcindia.hibernate;
import java.util.*;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
Transaction tx=null;
try {
SessionFactory sessionFactory= HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
session.close();
}
catch(Exception ex) {
tx.rollback();
ex.printStackTrace();
}
}
}
import java.util.*;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Lab3C {
public static void main(String[] args) {
Transaction tx=null;
try {
SessionFactory sessionFactory= 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();
}
4)Lab3D.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Lab3D {
public static void main(String[] args) {
Transaction tx=null;
try {
SessionFactory sessionFactory= HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
tx = session.beginTransaction();
import java.util.*;
import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@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 = "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 = "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 + "]";
}
}
Interview Questions:
Q19) What are the Laoading Strategies supported by Hibernate?
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:
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: