JPA2 + Hibernate 4: removing orphans (orphanRemoval = true) does not work with OneToOne

I have a problem with removing orphans in JPA 2. Inside the transaction, I create a one-to-one relationship target object (OrderDirectDebit), and then add it to the existing and constant DonationIntent (using the merge method). Then I delete this relation by folding it. Then I repeat again (use the merge method again).

I have orphanRemoval configured for a relationship, but still the object is not deleted. The spectrum says that the delete operation is applied when the flash operation is called (which should happen as soon as the transaction ends). But in my example, an orphan entity still exists after fixation. When I call flush manually after a merge operation (inside my custom persist method), the lost object is deleted. If I call a flash operation manually on another object manager that uses the same persistence context (from an interceptor), the lost object is not deleted.

I can not understand the meaning of behavior. Do I need to call to clear manually immediately after the merger? Does this make sense? Please comment. Here are my class definitions, storage persist method, and unit test code:

// this is a method of a stateless bean with transaction required // it is run inside an arquillian unit test public void testOrderCreation() { DonationIntent donation = repositoryDonationIntent.findById(id); Country country = testhelper.getCountryGermany(); BankAccount bankAccount =testhelper.getBankAccountForTestPerson1(); Assert.assertFalse(donation.createOrderDirectDebit(23.0d, country, OrderType.EState.NEW, bankAccount).hasErrors()); donation = repositoryDonationIntent.persist(donation); Assert.assertNotNull(donation.getOrder()); donation.setOrder(null); donation = repositoryDonationIntent.persist(donation); Assert.assertTrue(repositoryDonationIntent.findById(donation.getId()) == donation); Assert.assertNull(repositoryDonationIntent.findById(donation.getId()).getOrder()); //the assert verifies the the relation of the object in the persistence context is null,still after commit it does not remove it } 

Object Model:

  @Entity public class DonationIntent extends AutoIdDomainObject implements IAggregateRoot<Long> { @OneToOne(cascade = CascadeType.ALL, optional = true, orphanRemoval = true) private OrderType order; // this is the relation that does not get orphaned } @Entity @Inheritance(strategy = InheritanceType.JOINED) public abstract class OrderType extends AutoIdDomainObject { // some more attributes and relations here } @Entity public class OrderDirectDebit extends OrderType { // some more attributes and relations here } 

Persistent Repository Method:

  @Override public <D extends IDomainObject<?>> D persist(Class<D> domainClass, D domainObject) { //TODO Do not permit old revisions of versioned objects to be persisted if (domainObject == null) return null; // invalid domain objects may not be persisted if (!domainObject.isValid()) { DomainObjectErrorLogger.log(domainObject.getErrors()); return domainObject; } // check if update or insert if (isPersistent(domainClass, domainObject)) { domainObject = em.merge(domainObject); em.flush(); // a call to flush here will remove orphaned entity } else { em.persist(domainObject); } return domainObject; } 
+4
source share

Source: https://habr.com/ru/post/1389633/


All Articles