I have several repositories extending BaseRepository as follows:
public abstract class IsoRepository<T extends Serializable> {
@PersistenceContext
protected EntityManager entityManager;
public void persist(T obj) {
entityManager.persist(obj);
}
}
@Stateless
public class T1Repository extends BaseRepository<T1> {
// methods depending on repository
}
@Stateless
public class T2Repository extends BaseRepository<T2> {
public Optional<T2> findByOrderId(String id) {
return entityManager.createNamedQuery(/* ... */, T2.class)
.setParameter("id", id).getResultList().stream().findFirst();
}
}
// and others
EJB bean contains method reponsible for saving transaction:
@Stateless
public class TransactionService {
@EJB
private T2Repository t2Repository;
public void saveTransaction() {
// Here occurs logic that saves new entities to database via repositories injected with EJB annotation
// and also as the last operation update is performed:
T2 t2 = t2Repository.findById(id);
t2.setProperty(someProperty);
t2Repository.persist(t2);
}
}
The problem is that all insert queries are saved in the database, but not this one poor update. I found out that I need to call entityManager.flush()
explicitly, as it seems to solve the issue, but I do not really understand why is that happening. I've always thought that after transaction is committed all data is flushed automatically anyways. Do I have do change something in the configuration?