Well, I checked your test case and tried to "simulate" what Springs JtaTransactionManager.triggerBeforeCompletion() is doing. I added entityManager.close(); before the transaction is going to be commited to you test case and could reproduce the issue against Hibernate 5.3.1.Final. According to Jürgen Höllers comment at https://jira.spring.io/browse/SPR-15334, the "closed" or "opened" state in JPA is a user-level indication. However, the underlying session should be able to participate in the transaction commit phase even if the entity manager is closed for user interaction. Does this help to reproduce my problem? Thx a lot for your time and investigations, you are doing a great job!
@Test
public void addRelationImplicitFlush() throws Exception {
Long contractId = doInJPA( this::entityManagerFactory, entityManager -> {
Contract contract = new Contract();
entityManager.persist( contract );
return contract.getId();
} );
Long customerId = doInJPA( this::entityManagerFactory, entityManager -> {
Customer customer = new Customer();
entityManager.persist( customer );
return customer.getId();
} );
doInJPA( this::entityManagerFactory, entityManager -> {
Customer customer = entityManager.createQuery(
"SELECT c " +
"FROM Customer c " +
" LEFT JOIN FETCH c.contractRelations " +
" WHERE c.id = :customerId", Customer.class )
.setParameter( "customerId", customerId )
.getSingleResult();
CustomerContractRelation relation = new CustomerContractRelation();
relation.setContractId( contractId );
customer.addContractRelation( relation );
entityManager.close();
} );
}
|