Actually, removing the refresh() doesn't help. If I just call the setEmail method and then commit, I get the same result at SQL level, although the modifedAt field in the entity will be correct after the commit. IMHO, it is inconsistent at best. So, in this testcase, the entity is created and committed, then loaded, changed and committed, and finally loaded and checked.
try( UnitOfWork uow = persistenceTestHarness.newUnitOfWork() ) {
Customer cust = persistenceTestHarness.repository().create( Customer.class );
cust.setEmail( email1 );
persistenceTestHarness.repository().save( cust );
uow.complete();
}
Thread.sleep(10);
try( UnitOfWork uow = persistenceTestHarness.newUnitOfWork() ) {
CustomerQueries queries = persistenceTestHarness.repository().getQueries( CustomerQueries.class );
Optional<Customer> customerOptional = queries.findCustomerByEmailAndMerchantIdentity( email, merchantId ).find();
assertThat( customerOptional.isPresent(), equalTo( true ) );
Customer cust = customerOptional.get();
assertThat( cust.getEmail(), equalTo( email1 ) );
cust.setEmail( email2 );
uow.complete();
}
try( UnitOfWork uow = persistenceTestHarness.newUnitOfWork() ) {
CustomerQueries queries = persistenceTestHarness.repository().getQueries( CustomerQueries.class );
Optional<Customer> customerOptional = queries.findCustomerByEmailAndMerchantIdentity( email, merchantId ).find();
assertThat( customerOptional.isPresent(), equalTo( true ) );
Customer cust = customerOptional.orElse(null);
assertThat( cust, notNullValue() );
assertThat( cust.getEmail(), equalTo( email2 ) );
assertFalse( cust.getModifiedAt().equals( cust.getCreatedAt() ) );
uow.complete();
}
UnitOfWork.complete() in terms of EntityManager, only calls EntityManager.commit() The Queries uses JPA TypedQuery. |