create a class Dept.
java
------------------------------------
import java.util.List;
import java.util.Objects;
public class Dept {
private int deptno;
private String deptname;
private String loc;
private List<Emp> empList;
public Dept() {
super();
// TODO Auto-generated constructor stub
}
public Dept(int deptno, String deptname, String loc, List<Emp> empList) {
super();
this.deptno = deptno;
this.deptname = deptname;
this.loc = loc;
this.empList = empList;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public List<Emp> getEmpList() {
return empList;
}
public void setEmpList(List<Emp> empList) {
this.empList = empList;
}
@Override
public int hashCode() {
return Objects.hash(deptname, deptno, empList, loc);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dept other = (Dept) obj;
return Objects.equals(deptname, other.deptname) && deptno ==
other.deptno
&& Objects.equals(empList, other.empList) &&
Objects.equals(loc, other.loc);
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", deptname=" + deptname + ", loc=" +
loc + ", empList=" + empList
+ ", getDeptno()=" + getDeptno() + ", getDeptname()=" +
getDeptname() + ", getLoc()=" + getLoc()
+ ", getEmpList()=" + getEmpList() + ", hashCode()=" +
hashCode() + ", getClass()=" + getClass()
+ ", toString()=" + super.toString() + "]";
}
}
--------------------------------------
create another class named Emp.java
------------------------------------
import java.util.Objects;
public class Emp {
private int eid;
private String ename;
private double sal;
private String desig;
private Dept dept;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(int eid, String ename, double sal, String desig, Dept dept) {
super();
this.eid = eid;
this.ename = ename;
this.sal = sal;
this.desig = desig;
this.dept = dept;
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public String getDesig() {
return desig;
}
public void setDesig(String desig) {
this.desig = desig;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
@Override
public int hashCode() {
return Objects.hash(desig, eid, ename, sal);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Emp other = (Emp) obj;
return Objects.equals(desig, other.desig) && eid == other.eid &&
Objects.equals(ename, other.ename)
&& Double.doubleToLongBits(sal) ==
Double.doubleToLongBits(other.sal);
}
@Override
public String toString() {
return "Emp [eid=" + eid + ", ename=" + ename + ", sal=" + sal + ",
desig=" + desig + ",dept_name="+dept.getDeptname()+"]";
}
}
-----------------------------------------
create another class named DeptEmpSaveClient1.java
--------------
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class DeptEmpSaveClient1 {
public static void main(String[] args) {
Emp e1=new Emp(0, "acb", 100000.00, "Desi", null);
Emp e2=new Emp(0, "cdf", 100000.00, "Des", null);
Emp e3=new Emp(0, "rut", 100000.00, "Designer", null);
Emp e4=new Emp(0, "ruth", 100000.00, "Desig", null);
List<Emp> empList=new ArrayList<Emp>();
empList.add(e1);
Dept d1=new Dept(0, "pat", "Hyderbad", empList);
Session s=HSFactory.getSession();
Transaction t=s.beginTransaction();
try {
s.save(d1);
s.flush();
t.commit();
System.out.println("TX Success");
} catch (Exception e) {
t.rollback();
System.out.println("TX Failed");
e.printStackTrace();
}
}
}
-----------------------------------------
create another class named HSFactory.java
------------------------------
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class DeptEmpSaveClient1 {
public static void main(String[] args) {
Emp e1=new Emp(0, "acb", 100000.00, "Desi", null);
Emp e2=new Emp(0, "cdf", 100000.00, "Des", null);
Emp e3=new Emp(0, "rut", 100000.00, "Designer", null);
Emp e4=new Emp(0, "ruth", 100000.00, "Desig", null);
List<Emp> empList=new ArrayList<Emp>();
empList.add(e1);
Dept d1=new Dept(0, "pat", "Hyderbad", empList);
Session s=HSFactory.getSession();
Transaction t=s.beginTransaction();
try {
s.save(d1);
s.flush();
t.commit();
System.out.println("TX Success");
} catch (Exception e) {
t.rollback();
System.out.println("TX Failed");
e.printStackTrace();
}
}
}
--------------------------
now create file named Emp.hbm.xml
----------------------
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class DeptEmpSaveClient1 {
public static void main(String[] args) {
Emp e1=new Emp(0, "acb", 100000.00, "Desi", null);
Emp e2=new Emp(0, "cdf", 100000.00, "Des", null);
Emp e3=new Emp(0, "rut", 100000.00, "Designer", null);
Emp e4=new Emp(0, "ruth", 100000.00, "Desig", null);
List<Emp> empList=new ArrayList<Emp>();
empList.add(e1);
Dept d1=new Dept(0, "pat", "Hyderbad", empList);
Session s=HSFactory.getSession();
Transaction t=s.beginTransaction();
try {
s.save(d1);
s.flush();
t.commit();
System.out.println("TX Success");
} catch (Exception e) {
t.rollback();
System.out.println("TX Failed");
e.printStackTrace();
}
}
}
--------------------------
create another file named hibernate.cfg.xml
----------------------------
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class DeptEmpSaveClient1 {
public static void main(String[] args) {
Emp e1=new Emp(0, "acb", 100000.00, "Desi", null);
Emp e2=new Emp(0, "cdf", 100000.00, "Des", null);
Emp e3=new Emp(0, "rut", 100000.00, "Designer", null);
Emp e4=new Emp(0, "ruth", 100000.00, "Desig", null);
List<Emp> empList=new ArrayList<Emp>();
empList.add(e1);
Dept d1=new Dept(0, "pat", "Hyderbad", empList);
Session s=HSFactory.getSession();
Transaction t=s.beginTransaction();
try {
s.save(d1);
s.flush();
t.commit();
System.out.println("TX Success");
} catch (Exception e) {
t.rollback();
System.out.println("TX Failed");
e.printStackTrace();
}
}
}
--------------------------