Hello. It will be nice if someone helps me.
After upgrading to the latest hibernate version (4.1.4) from the version 3.5.6 some unit tests started to fail because of the following exception:
Code:
java.lang.UnsupportedOperationException: Can't write to a readonly object
In a unit test I just persist some entities using entityManager.persist and then load them using entityManager.find. These entities are configured to use read-only second level cache (ehcache).
Simple example:
Code:
Brand brand = new Brand();
entityManager.persist(brand);
entityManager.flush();
entityManager.clear();
Brand brandFound= entityManager.find(Brand.class, brand.getId());
assertEquals(brand, brandFound);
The problem that the
org.hibernate.engine.internal.TwoPhaseLoad class was updated in a scope of the
https://hibernate.onjira.com/browse/HHH-5490:
Code:
// explicit handling of caching for rows just inserted and then somehow forced to be read
// from the database *within the same transaction*. usually this is done by
// 1) Session#refresh, or
// 2) Session#clear + some form of load
//
// we need to be careful not to clobber the lock here in the cache so that it can be rolled back if need be
if ( session.getPersistenceContext().wasInsertedDuringTransaction( persister, id ) ) {
persister.getCacheAccessStrategy().update(
cacheKey,
persister.getCacheEntryStructure().structure( entry ),
version,
version
);
}
As a unit test runs in one transaction the
persister.getCacheAccessStrategy().update line is executed what leads to the described exception because the
ReadOnlyEhcacheEntityRegionAccessStrategy is used for an entity.
Earlier in the hibernate 3.5.6 the
putFromLoad method was used and everything worked perfectly.
So I'm just not sure what to do. Is this really a correct behavior? Isn't it strange that when you're reading entity using entityManager.find you should make sure that it hasn't been inserted in a same transaction (if we use read only cache for it). As it's still allowed to persist new entities configured with second level read-only cache - this seems to be a bug.
Thanks in advance for any response.