I don’t think the transaction is suspended. If i change my test as follows (each method excpet the test itself is annotated with @Transactional(Transactional.TxType.REQUIRES_NEW)):
@Test
public void shouldCreateUpdateAndSelectMyEntity() throws Exception {
insert();
findAndUpdate();
selectFiltered();
}
@Transactional(Transactional.TxType.REQUIRES_NEW)
public void insert() {
MyEntity myEntity = new MyEntity();
myEntity.setId(PK);
MyJson myJson = new MyJson();
myJson.setLongProp(100L);
myJson.setStringProp("Hello");
myEntity.setJsonProperty(myJson);
em.persist(myEntity);
}
@Transactional(Transactional.TxType.REQUIRES_NEW)
public void findAndUpdate() throws Exception {
MyEntity found = em.find(MyEntity.class, PK);
found.getJsonProperty().setStringProp(CHANGED);
}
@Transactional(Transactional.TxType.REQUIRES_NEW)
public void selectFiltered() {
List<MyEntity> result = em
.createQuery("SELECT e FROM MyEntity e WHERE e.jsonProperty.longProp = :x", MyEntity.class)
.setParameter("x", 100L)
.getResultList();
assertEquals(1, result.size());
assertEquals(CHANGED, result.get(0).getJsonProperty().getStringProp(), "json property not changed");
}
And step over the methods and select after each method with an external sql-navigator-tool , what’s currently in the DB, i find immediatly after the insert my data as expected in the table. So it seems the transaction was commited after the insert-method was done. I expect the new transactio for the findAndUpdate-Method to do likewise. Another hint: Even if i change the findAndUpdate-Method to force a flush
@Transactional(Transactional.TxType.REQUIRES_NEW)
public void findAndUpdate() throws Exception {
MyEntity found = em.find(MyEntity.class, PK);
found.getJsonProperty().setStringProp(CHANGED);
em.flush();
}
Nothing is changed on the DB. I assume it’s because the flush is here a no-op operation (for hibernate entity is not dirty → no update necessary) If i force the entity to be considered dirty by manipulating the other ‘info’-property
@Transactional(Transactional.TxType.REQUIRES_NEW)
public void findAndUpdate() throws Exception {
MyEntity found = em.find(MyEntity.class, PK);
found.getJsonProperty().setStringProp(CHANGED);
found.setInfo(CHANGED); }
and again step through the methods, if see the changes fo json and info in the DB using the external SQL-tool, immediatly after ‘findAndUpdate’ has been finished. |