When an entity has a database generated identity and an immutable natural identity, identity values are supposed to be cached in second level cache when configured. There seems to be a bug in public void org.hibernate.action.internal.AbstractEntityInsertAction.handleNaturalIdPostSaveNotifications(Serializable generatedId) method. It receives a valid generated Id value, but disregards it when calling PersistenceContext$NaturalIdHelper.manageSharedNaturalIdCrossReference(...). It passes getId() method result which evaluates to null. This causes null id value to be cached against the natural id value. Then, every time the entity is looked up by the natural id value, cache returns null. Identity is looked up in the database, but not cached, because cache access strategy (ReadWriteEhcacheNaturalIdRegionAccessStrategy) never updates cache entries (they supposed to immutable). In our system these lookups increase the database load dramatically. |