Sun Application Server 9.1
PostgreSQL 8.2
I have Stateless session bean methods "save" and "findById". The save method persists the passed entity, flushes the entity manager, and then refreshes the entity. One of the columns (not the primary key) in the underlying table is modified by a database trigger on before insert. I need to make sure this value makes it back to the entity, so after I persist(), I do flush(), then refresh().
I would expect that the next time I call findById on the entity manager in the same persistence context with the ID from the entity above, that the value from the trigger modified column would be present, but this is not the case. The value in the entity representing that column is null. The entity manager refresh() seems as though it is not working. I have even tried calling clear() on the entity manager to try to clean out any cached entities after persisting, but this didn't work either.
If I query the database directly with a querying tool, the value is present in the column, and if I call findById in a different persistence context with the primary key from above, the returned entity contains the trigger-updated value from the database. So, the trigger is working correctly, and the entity is correctly mapped.
Am wrong in thinking that refresh() should re-query the entity from the database and get rid of anything cached in the persistence context? Am I doing something wrong?
My 2 stateless bean methods are below.
Thanks in advance for any help,
Kurzweil4
public void save( CsaCustomers entity ) {
LogUtil.log( "saving CsaCustomers instance", Level.INFO, null );
try {
entityManager.persist( entity );
entityManager.flush();
entityManager.refresh( entity );
LogUtil.log( "save successful", Level.INFO, null );
}
catch ( RuntimeException re ) {
LogUtil.log( "save failed", Level.SEVERE, re );
throw re;
}
}
public CsaCustomers findById( Long id ) {
try {
CsaCustomers instance = entityManager.find( CsaCustomers.class, id );
return instance;
}
catch ( RuntimeException re ) {
throw re;
}
}