public
class
TestOrphanRemovalCascade
{
public
static
void
main(String[] args)
{
setupTestData();
Session sessionOne = HibernateUtil.getSessionFactory().openSession();
org.hibernate.Transaction tx = sessionOne.beginTransaction();
EmployeeEntity employee = (EmployeeEntity) sessionOne.load(EmployeeEntity.
class
,
1
);
System.out.println(
"Step 1 : "
+ employee.getAccounts().size());
employee.getAccounts().remove(employee.getAccounts().iterator().next());
System.out.println(
"Step 2 : "
+ employee.getAccounts().size());
tx.commit();
sessionOne.close();
Session sessionTwo = HibernateUtil.getSessionFactory().openSession();
sessionTwo.beginTransaction();
EmployeeEntity employee1 = (EmployeeEntity) sessionTwo.load(EmployeeEntity.
class
,
1
);
System.out.println(
"Step 3 : "
+ employee1.getAccounts().size());
Query query = sessionTwo.createQuery(
"from Account a"
);
@SuppressWarnings
(
"unchecked"
)
List<AccountEntity> accounts = query.list();
System.out.println(
"Step 4 : "
+ accounts.size());
sessionTwo.close();
HibernateUtil.shutdown();
}
private
static
void
setupTestData(){
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
EmployeeEntity emp =
new
EmployeeEntity();
emp.setEmployeeId(
1
);
emp.setFirstName(
"Lokesh"
);
emp.setLastName(
"Gupta"
);
session.save(emp);
AccountEntity acc1 =
new
AccountEntity();
acc1.setAccountId(
1
);
acc1.setAccountNumber(
"11111111"
);
acc1.setEmployee(emp);
session.save(acc1);
AccountEntity acc2 =
new
AccountEntity();
acc2.setAccountId(
2
);
acc2.setAccountNumber(
"2222222"
);
acc2.setEmployee(emp);
session.save(acc2);
AccountEntity acc3 =
new
AccountEntity();
acc3.setAccountId(
3
);
acc3.setAccountNumber(
"33333333"
);
acc3.setEmployee(emp);
session.save(acc3);
session.getTransaction().commit();
session.close();
}
}
Output:
Hibernate: insert into Employee (FIRST_NAME, LAST_NAME, ID) values (?, ?, ?)
Hibernate: insert into Account (ACC_NO, employee_ID, ID) values (?, ?, ?)
Hibernate: insert into Account (ACC_NO, employee_ID, ID) values (?, ?, ?)
Hibernate: insert into Account (ACC_NO, employee_ID, ID) values (?, ?, ?)
Hibernate: select employeeen0_.ID as ID1_1_0_, employeeen0_.FIRST_NAME as FIRST_NA2_1_0_, employeeen0_.LAST_NAME as
LAST_NAM3_1_0_ from Employee employeeen0_ where employeeen0_.ID=?
Hibernate: select accounts0_.employee_ID as employee3_1_0_, accounts0_.ID as ID1_0_0_, accounts0_.ID as ID1_0_1_,
accounts0_.ACC_NO
as ACC_NO2_0_1_, accounts0_.employee_ID as employee3_0_1_ from Account
accounts0_ where accounts0_.employee_ID=?
Step
1
:
3
Step
2
:
2
Hibernate: delete from Account where ID=?
Hibernate: select employeeen0_.ID as ID1_1_0_, employeeen0_.FIRST_NAME as FIRST_NA2_1_0_, employeeen0_.LAST_NAME as
LAST_NAM3_1_0_ from Employee employeeen0_ where employeeen0_.ID=?
Hibernate: select accounts0_.employee_ID as employee3_1_0_, accounts0_.ID as ID1_0_0_, accounts0_.ID as ID1_0_1_,
accounts0_.ACC_NO
as ACC_NO2_0_1_, accounts0_.employee_ID as employee3_0_1_ from Account
accounts0_ where accounts0_.employee_ID=?
Step
3
:
2
Hibernate: select accountent0_.ID as ID1_0_, accountent0_.ACC_NO as ACC_NO2_0_, accountent0_.employee_ID as employee3_0_
from Account accountent0_
Step
4
:
2