Merge pull request #10216 from emyasa/BAEL-4643
BAEL-4643: JPA CascadeType.REMOVE vs orphanRemoval
This commit is contained in:
commit
0baaeb3f98
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.jpa.removal;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import java.util.Objects;
|
||||
|
||||
@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() {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.jpa.removal;
|
||||
|
||||
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;
|
||||
import java.util.List;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
public void setLineItems(List<LineItem> lineItems) {
|
||||
this.lineItems = lineItems;
|
||||
}
|
||||
|
||||
protected OrderRequest() {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
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() {
|
||||
}
|
||||
}
|
|
@ -1,25 +1,43 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
|
||||
version="2.2">
|
||||
<persistence-unit name="jpa-h2-equality">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.equality.EqualByJavaDefault</class>
|
||||
<class>com.baeldung.jpa.equality.EqualById</class>
|
||||
<class>com.baeldung.jpa.equality.EqualByBusinessKey</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>
|
||||
version="2.2">
|
||||
<persistence-unit name="jpa-h2-equality">
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
<class>com.baeldung.jpa.equality.EqualByJavaDefault</class>
|
||||
<class>com.baeldung.jpa.equality.EqualById</class>
|
||||
<class>com.baeldung.jpa.equality.EqualByBusinessKey</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-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>
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.jpa.removal;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
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 java.util.List;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package com.baeldung.jpa.removal;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import javax.persistence.PersistenceException;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class OrphanRemovalIntegrationTest {
|
||||
|
||||
private static EntityManagerFactory factory;
|
||||
private static EntityManager entityManager;
|
||||
|
||||
@Before
|
||||
public 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());
|
||||
}
|
||||
|
||||
@Test(expected = PersistenceException.class)
|
||||
public void whenLineItemsIsReassigned_thenThrowAnException() {
|
||||
createOrderRequestWithLineItems();
|
||||
|
||||
OrderRequest orderRequest = entityManager.find(OrderRequest.class, 1L);
|
||||
orderRequest.setLineItems(new ArrayList<>());
|
||||
|
||||
entityManager.getTransaction().begin();
|
||||
entityManager.merge(orderRequest);
|
||||
entityManager.getTransaction().commit();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue