I have an Entity
that, simplified, looks like this:
@Entity
@Table(name = "SOMETABLE")
public class SomeEntity {
// real code has id and more columns
@Column(name = "SOMECOLUMN")
private String someColumn;
@Transient
private SomeObject transientObject;
// getters and setters
}
A DAO method loads a list of entities by using a @NamedQuery
and a JPA EntityManager
(roughly stubbed):
@Transactional
public List<SomeEntity> getSomeEntities() {
TypedQuery<SomeEntity> query = entityManager.createNamedQuery("findSomeEntities", SomeEntity.class);
List<SomeEntity> someEntities = query.getResultList();
for (SomeEntity someEntity : someEntities) {
someEntity.setTransientObject(<some value here>);
return someEntities;
}
}
Note that this method also sets transientObject
(simplified in the code example).
The next time getSomeEntities()
is called, query.getResultList();
returns a list of objects where transientObject
is still set. I would expect the transient object to be null, but it is not. There are no first or second level caches enabled.
To further confuse this, this only happens during unit testing, where we use a HSQL in-memory database. When running the web application on a Tomcat server it works fine.
I debugged a bit and I found that in the session cache (which I understand is always enabled for Hibernate) seems to be loaded with all previously loaded objects when running the unit test, but it was empty when running on the application server. I suspect that this means that hibernate fetches the objects from the cache instead of the database.
Also worth to know is that it is a Spring application.
What is the reason for this? Or to rephrase my main problem: Why is transient object not null when loading entity second time using HSQLDB?