0% found this document useful (0 votes)
6 views12 pages

Module 2 - Collection Mapping

use ful content for java developers
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)
6 views12 pages

Module 2 - Collection Mapping

use ful content for java developers
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/ 12

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 cname;

Java Learning Center 17 Hibernate Study Guide


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

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

Java Learning Center 18 Hibernate Study Guide


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.jlcindia.hibernate;

import java.util.*;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

public class Lab3A {


public static void main(String[] args) {

Transaction tx=null;
try {

SessionFactory sessionFactory= 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);

Java Learning Center 19 Hibernate Study Guide


Set<String> interviews = new HashSet<>();
interviews.add("InfoSys");
interviews.add("TCS");
interviews.add("Google");
interviews.add("Amazon");

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("Sri","Sri@jlc",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.jlcindia.hibernate;

import java.util.*;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */

Java Learning Center 20 Hibernate Study Guide


public class Lab3B {
public static void main(String[] args) {

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

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


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

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();
}
}
}

Java Learning Center 21 Hibernate Study Guide


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

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(); //***

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


stu.setEmail("sri@jlc");
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();
}

Java Learning Center 22 Hibernate Study Guide


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

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();

//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();
}
}
}

Java Learning Center 23 Hibernate Study Guide


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

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;

Java Learning Center 24 Hibernate Study Guide


@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 + "]";
}
}

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

Q20) What is the Aggresive Loading?


Ans:

Java Learning Center 25 Hibernate Study Guide


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 Fileds 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:

Q29) How to map the List/Set/Map type Fileds of Entity Class?


Ans:

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


Ans:

Java Learning Center 26 Hibernate Study Guide


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:
Q38) What is Cascading?
Ans:

Java Learning Center 27 Hibernate Study Guide


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:

Java Learning Center 28 Hibernate Study Guide

You might also like