Hibernate One-To-Many Using Annotation:
***********************************
Files required are :
Employee.java
Address.java
hibernate.cfg.xml
SaveData.java
I am defining one parent (Employee.java) and one child class (Address.java).
Logic : Define one of the Collections i.e. Set in parent class , child objects needs to be added to this Set, associated with Employee, persist Employee object and commit the data.
Employee.java
===========
package mypackage;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@Column(name = "empid")
private int empId;
@Column(name = "empName")
private String empName;
@OneToMany(targetEntity = Address.class,cascade = CascadeType.ALL)
@JoinColumn(name = "empId_fk" , referencedColumnName = "empid")
// empId_fk is foreign key for empid defined in Employee class.
private Set address;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Set getAddress() {
return address;
}
public void setAddress(Set address) {
this.address = address;
}
}
***********************************
Files required are :
Employee.java
Address.java
hibernate.cfg.xml
SaveData.java
I am defining one parent (Employee.java) and one child class (Address.java).
Logic : Define one of the Collections i.e. Set in parent class , child objects needs to be added to this Set, associated with Employee, persist Employee object and commit the data.
Employee.java
===========
package mypackage;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@Column(name = "empid")
private int empId;
@Column(name = "empName")
private String empName;
@OneToMany(targetEntity = Address.class,cascade = CascadeType.ALL)
@JoinColumn(name = "empId_fk" , referencedColumnName = "empid")
// empId_fk is foreign key for empid defined in Employee class.
private Set address;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Set getAddress() {
return address;
}
public void setAddress(Set address) {
this.address = address;
}
}
Address.java
==========
package mypackage;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@Column(name = "addrId")
private int addrId;
@Column(name = "addrName")
private String addrName;
public int getAddrId() {
return addrId;
}
public void setAddrId(int addrId) {
this.addrId = addrId;
}
public String getAddrName() {
return addrName;
}
public void setAddrName(String addrName) {
this.addrName = addrName;
}
}
hibernate,cfg,xml
=============
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/technology</property>
<property name="hibernate.connection.username">app</property>
<property name="hibernate.connection.password">app</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="mypackage.Address" />
<mapping class="mypackage.Employee" />
</session-factory>
</hibernate-configuration>
SaveData.java
===========
===========
package mypackage;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.cfg.*;
import org.hibernate.*;
public class SaveData {
public static void main(String[] args){
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Employee emp = new Employee();
Address addr1 = new Address();
Address addr2 = new Address();
addr1.setAddrId(11111);
addr1.setAddrName("Mohan1111");
addr2.setAddrId(33333);
addr2.setAddrName("Mohan3333");
emp.setEmpId(22222);
emp.setEmpName("Bengaluru");
Set set = new HashSet();
set.add(addr1);
set.add(addr2);
emp.setAddress(set);
session.save(emp);
tx.commit();
session.close();
factory.close();
System.out.println("Saved the data successfully...");
}
}
No comments:
Post a Comment