BAEL-4643: JPA CascadeType.REMOVE vs orphanRemoval

* Defines the Domain: ShipmentInfo, LineItem and OrderRequest entity
* Adds CascadeTypeRemoveIntegrationTest
* Adds OrphanRemovalIntegrationTest
This commit is contained in:
Emmanuel Yasa 2020-10-29 00:16:53 +08:00
parent 5116a45d1e
commit 06c800c526
6 changed files with 270 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package com.baeldung.jpa.removal;
import java.util.Objects;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class LineItem {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToOne
private OrderRequest orderRequest;
public LineItem(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LineItem lineItem = (LineItem) o;
return Objects.equals(id, lineItem.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
protected LineItem() {}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.jpa.removal;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@Entity
public class OrderRequest {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(cascade = { CascadeType.REMOVE, CascadeType.PERSIST })
private ShipmentInfo shipmentInfo;
@OneToMany(orphanRemoval = true, cascade = CascadeType.PERSIST, mappedBy = "orderRequest")
private List<LineItem> lineItems;
public OrderRequest(ShipmentInfo shipmentInfo) {
this.shipmentInfo = shipmentInfo;
}
public OrderRequest(List<LineItem> lineItems) {
this.lineItems = lineItems;
}
public void removeLineItem(LineItem lineItem) {
lineItems.remove(lineItem);
}
protected OrderRequest() {}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.jpa.removal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class ShipmentInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public ShipmentInfo(String name) {
this.name = name;
}
protected ShipmentInfo() {}
}

View File

@ -22,4 +22,22 @@
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
</properties>
</persistence-unit>
<persistence-unit name="jpa-h2-removal">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.baeldung.jpa.removal.ShipmentInfo</class>
<class>com.baeldung.jpa.removal.LineItem</class>
<class>com.baeldung.jpa.removal.OrderRequest</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="false" />
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
</properties>
</persistence-unit>
</persistence>

View File

@ -0,0 +1,72 @@
package com.baeldung.jpa.removal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class CascadeTypeRemoveIntegrationTest {
private static EntityManagerFactory factory;
private static EntityManager entityManager;
@BeforeClass
public static void setup() {
factory = Persistence.createEntityManagerFactory("jpa-h2-removal");
entityManager = factory.createEntityManager();
}
@Test
public void whenOrderRequestIsDeleted_thenDeleteShipmentInfo() {
createOrderRequestWithShipmentInfo();
OrderRequest orderRequest = entityManager.find(OrderRequest.class, 1L);
entityManager.getTransaction().begin();
entityManager.remove(orderRequest);
entityManager.getTransaction().commit();
Assert.assertEquals(0, findAllOrderRequest().size());
Assert.assertEquals(0, findAllShipmentInfo().size());
}
private void createOrderRequestWithShipmentInfo() {
ShipmentInfo shipmentInfo = new ShipmentInfo("name");
OrderRequest orderRequest = new OrderRequest(shipmentInfo);
entityManager.getTransaction().begin();
entityManager.persist(orderRequest);
entityManager.getTransaction().commit();
Assert.assertEquals(1, findAllOrderRequest().size());
Assert.assertEquals(1, findAllShipmentInfo().size());
}
private List<OrderRequest> findAllOrderRequest() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<OrderRequest> cq = cb.createQuery(OrderRequest.class);
Root<OrderRequest> root = cq.from(OrderRequest.class);
CriteriaQuery<OrderRequest> findAll = cq.select(root);
TypedQuery<OrderRequest> findAllQuery = entityManager.createQuery(findAll);
return findAllQuery.getResultList();
}
private List<ShipmentInfo> findAllShipmentInfo() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<ShipmentInfo> cq = cb.createQuery(ShipmentInfo.class);
Root<ShipmentInfo> root = cq.from(ShipmentInfo.class);
CriteriaQuery<ShipmentInfo> findAll = cq.select(root);
TypedQuery<ShipmentInfo> findAllQuery = entityManager.createQuery(findAll);
return findAllQuery.getResultList();
}
}

View File

@ -0,0 +1,78 @@
package com.baeldung.jpa.removal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class OrphanRemovalIntegrationTest {
private static EntityManagerFactory factory;
private static EntityManager entityManager;
@BeforeClass
public static void setup() {
factory = Persistence.createEntityManagerFactory("jpa-h2-removal");
entityManager = factory.createEntityManager();
}
@Test
public void whenLineItemIsRemovedFromOrderRequest_thenDeleteOrphanedLineItem() {
createOrderRequestWithLineItems();
OrderRequest orderRequest = entityManager.find(OrderRequest.class, 1L);
LineItem lineItem = entityManager.find(LineItem.class, 2L);
orderRequest.removeLineItem(lineItem);
entityManager.getTransaction().begin();
entityManager.merge(orderRequest);
entityManager.getTransaction().commit();
Assert.assertEquals(1, findAllOrderRequest().size());
Assert.assertEquals(2, findAllLineItem().size());
}
private void createOrderRequestWithLineItems() {
List<LineItem> lineItems = new ArrayList<>();
lineItems.add(new LineItem("line item 1"));
lineItems.add(new LineItem("line item 2"));
lineItems.add(new LineItem("line item 3"));
OrderRequest orderRequest = new OrderRequest(lineItems);
entityManager.getTransaction().begin();
entityManager.persist(orderRequest);
entityManager.getTransaction().commit();
Assert.assertEquals(1, findAllOrderRequest().size());
Assert.assertEquals(3, findAllLineItem().size());
}
private List<OrderRequest> findAllOrderRequest() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<OrderRequest> cq = cb.createQuery(OrderRequest.class);
Root<OrderRequest> root = cq.from(OrderRequest.class);
CriteriaQuery<OrderRequest> findAll = cq.select(root);
TypedQuery<OrderRequest> findAllQuery = entityManager.createQuery(findAll);
return findAllQuery.getResultList();
}
private List<LineItem> findAllLineItem() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<LineItem> cq = cb.createQuery(LineItem.class);
Root<LineItem> root = cq.from(LineItem.class);
CriteriaQuery<LineItem> findAll = cq.select(root);
TypedQuery<LineItem> findAllQuery = entityManager.createQuery(findAll);
return findAllQuery.getResultList();
}
}