package com.example;
import static org.junit.Assert.assertNull;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestParentChildRelationship {
private EntityManagerFactory emf;
private EntityManager em;
@Before
public void setup() {
emf = JpaConfig.getEntityManagerFactory();
em = emf.createEntityManager();
}
@After
public void tearDown() {
if (em != null) {
em.close();
}
if (emf != null) {
emf.close();
}
}
@Test
public void testParentChildRelationship() {
EntityTransaction transaction = em.getTransaction();
transaction.begin();
// Create Parent entity 1
ParentEntity parentEntity1 = new ParentEntity();
parentEntity1.setId(1);
parentEntity1.setName("parent entity 1");
ChildEntity childEntity1 = new ChildEntity();
childEntity1.setId(1);
childEntity1.setName("Child of parent entity 1");
parentEntity1.setChildEntity(childEntity1);
em.persist(parentEntity1);
// Create Parent entity 2
ParentEntity parentEntity2 = new ParentEntity();
parentEntity2.setId(2);
parentEntity2.setName("parent entity 2");
ChildEntity childEntity2 = new ChildEntity();
childEntity2.setId(2);
childEntity2.setName("Child of parent entity 2");
parentEntity2.setChildEntity(childEntity2);
em.persist(parentEntity2);
transaction.commit();
// Delete using EM
int parentId = parentEntity1.getId();
transaction.begin();
ParentEntity retrievedParent = em.find(ParentEntity.class, parentId);
em.remove(retrievedParent);
transaction.commit();
// Delete using HQL
parentId = parentEntity2.getId();
transaction = em.getTransaction();
transaction.begin();
// Use HQL to delete the parent .... with cascade all, the child should be deleted.
Query query = em.createQuery("DELETE FROM ParentEntity WHERE id = :parentId");
query.setParameter("parentId", parentId);
query.executeUpdate();
transaction.commit();
assertNull(
"Parent entity 1 should be deleted", em.find(ParentEntity.class, parentEntity1.getId()));
assertNull(
"Parent entity 2 should be deleted", em.find(ParentEntity.class, parentEntity2.getId()));
assertNull(
"Child entity 1 should be deleted along with the parent",
em.find(ChildEntity.class, childEntity1.getId()));
assertNull(
"Child entity 2 should be deleted along with the parent",
em.find(ChildEntity.class, childEntity2.getId()));
}
}