I have problem with my Rest API. When I try to do findById or other fetch operations after some update or save methods, my JPA do not update this data in API but in database this data is updated. So can I dont user flush and clear before fetching or after adding date to DB. Because I think its not right code.
protected ENTITY findById(Long id, Class<ENTITY> entityClass) {
try {
tx.begin();
entityManager.flush();
entityManager.clear();
final ENTITY entity = entityManager.find(entityClass, id);
tx.commit();
if (entity == null) {
log.warn(entityClass.getSimpleName() + " is null");
return null;
}
return entity;
} catch (final HibernateException ex) {
tx.rollback();
throw new DaoException("Cannot find entity by id", ex);
}
}
This is my update method
protected ENTITY update(ENTITY entity) {
try {
tx.begin();
entityManager.merge(entity);
tx.commit();
return entity;
} catch (final HibernateException ex) {
tx.rollback();
throw new DaoException("Cannot update entity", ex);
}
}