Module 4 - Association Mapping (1)
Module 4 - Association Mapping (1)
If you want to establish the relationship among different Entity classes, then you
have to use Association Mapping.
When there is IS-A Relation between two Entities then follow Inheritance Mapping.
Ex:
GoldCustomer IS A Customer
SilverCustomer IS A Customer.
When there is HAS-A Relation between two Entities then follow Association
Mapping.
Ex:
Customer HAS A Address
Customer HAS A Order
1) One-One Relationship
3) Many-Many Relationship
A)Tables Required
1) customers
cid cname email phone myaddId
1 Sri [email protected] 99999
2) addresses
1) Customer.java
@Entity
@Table(name="customers")
public class Customer{
…
@OneToOne
@JoinColumn(name="myaddId")
Address address;
}
2)Address.java
@Entity
@Table(name="addresses")
class Address{
....
}
1)Lab7A.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
}
2)Lab7B.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
}
3)Lab7C.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Lab7C {
public static void main(String[] args) {
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
cust.setAddress(add); //IMP
tx.commit();
} catch (Exception ex) {
4)Lab7D.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Lab7D {
public static void main(String[] args) {
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
/*
// Not Provided Address
Customer cust = new Customer("aaa", "aaa@jlc", 111);
session.save(cust);
*/
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
} finally {
if (session != null)
session.close();
}
5)Lab7E.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
cust.setAddress(add); //IMP
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
}
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
Address add=cust.getAddress();
System.out.println(add);
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
}
7)Lab7G.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
}
8)Lab7H.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
}
9)Customer.java
package com.jlcindia.hibernate;
import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@Entity
@Table(name="mycustomers")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="cid")
private int cid;
@Column(name="cname")
private String cname;
@Column(name="email")
private String email;
@Column(name="phone")
private int phone;
public Customer(int cid, String cname, String email, int phone,Address address) {
this.cid = cid;
this.cname = cname;
this.email = email;
this.phone = phone;
this.address=address;
}
//Constrcutors
//Setters and Getters
//toString()
}
import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@Entity
@Table(name="myaddresses")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="addId")
private int addId;
@Column(name="street")
private String street;
@Column(name="city")
private String city;
@Column(name="state")
private String state;
public Address() {}
//Constrcutors
//Setters and Getters
//toString()
A)Tables Required
1) customers
cid cname email phone myaddId
1 Sri [email protected] 99999
2) addresses
1) Customer.java
@Entity
@Table(name="customers")
public class Customer{
…
@OneToOne
@JoinColumn(name="myaddId")
Address address;
}
2)Address.java
@Entity
@Table(name="addresses")
class Address{
....
@OneToOne(mappedBy = "address")
Customer customer;
7)Lab8G.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
public class Lab7G {
public static void main(String[] args) {
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
9)Lab8I.java
package com.jlcindia.hibernate;
import org.hibernate.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
Transaction tx = null;
Session session = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
} catch (Exception ex) {
tx.rollback();
ex.printStackTrace();
} finally {
if (session != null)
session.close();
}
}
}
import javax.persistence.*;
/*
* @Author : Srinivas Dande
* @Company: Java Learning Center
* */
@Entity
@Table(name="myaddresses")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="addId")
private int addId;
@Column(name="street")
private String street;
@Column(name="city")
private String city;
@Column(name="state")
private String state;
public Address() {}
public Address(String street, String city, String state) {
this.street = street;
this.city = city;
this.state = state;
}
public Address(int addId, String street, String city, String state) {
this.addId = addId;
this.street = street;
this.city = city;
this.state = state;
}
//Constrcutors
//Setters and Getters
//toString()
}
Q77) What are the Annotations to be used for First Side Entity Class in One-One Mapping?
Ans:
Q78) What are the Annotations to be used for Second Side Entity Class in One-One Mapping?
Ans:
Q80) What is the Default Loading Strategy used for One-One Mapping?
Ans:
Q82) What is the Default Cascade Style used for One-One Mapping?
Ans:
Q83) How to specify the Required Loading Strategy used for One-One Mapping?
Ans:
Q84) How to specify the Required Fetching Strategy used for One-One Mapping?
Ans:
Q85) How to specify the Required Cascade Style used for One-One Mapping?
Ans:
A)Tables Required
1) mycustomers
cid cname email phone
1 Sri [email protected] 99999
2) myaccounts
accno atype acode bal mycid
1 SA BC-109 20000 1
2 DA BC-199 25000 1
1) Customer.java
@Entity
@Table(name = "mycustomers")
public class Customer {
…
@OneToMany(mappedBy = "customer")
private Set< Account > accounts;
...
}
2) Account.java
@Entity
@Table(name = "myaccounts")
public class Account {
…
@ManyToOne
@JoinColumn(name="mycid",referencedColumnName="cid")
Customer customer;
...
}
1. Lab9A.java 2. Lab9B.java
package com.jlcindia.hibernate; package com. jlcindia.hibernate;
}
}
A)Tables Required
1) mystudents
sid sname email phone
2) mycourses
cid cname cost trainer
3) stu_cou
mycid mysid
2)Course.java
@Entity
@Table(name = "jlccourses")
public class Course {
@ManyToMany(mappedBy = "courses")
private Set<Student> students;
}
1. Lab10A.java 2. Lab10B.java
package com.coursecube.hibernate; package com.coursecube.hibernate;
tx.commit();
session.close();
}catch(Exception ex) {
ex.printStackTrace();
if(tx!=null)
tx.rollback();
} }
}
3. Lab10C.java 4. Lab10D.java
package com.coursecube.hibernate; package com.coursecube.hibernate;
stu.setCourses(cous); tx.commit();
session.close();
5. Lab10E.java 6. Lab10F.java
package com.coursecube.hibernate; package com.coursecube.hibernate;
tx.commit(); tx.commit();
session.close(); session.close();
}catch(Exception ex) { }catch(Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
if(tx!=null) if(tx!=null)
tx.rollback(); tx.rollback();
} }
} }
} }
tx.commit(); tx.commit();
session.close(); session.close();
}catch(Exception ex) { }catch(Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
if(tx!=null) if(tx!=null)
tx.rollback(); tx.rollback();
} }
} }
} }
@Column(name="sname") @Column(name="cname")
private String sname; private String cname;
@Column(name="email") @Column(name="cost")
private String email; private double cost;
@Column(name="phone") @Column(name="trainer")
private long phone; private String trainer;