Just implemented and tested a quick and dirty workaround with a custom PostInsertEventListener registered with the post-commit-insert event. This seems to work fine so far as the entity is now put in the second-level cache on insert.
I don't know what this breaks though, so of course I don't want to use this in production code (clearly there must be a reason why this isn't done in the hibernate core code and the according parts are commented out there, see also EntityIdentityInsertAction.doAfterTransactionCompletion()). Apart from this the post-commit-insert listener is also called for rolled-back transactions, which is another issue (HHH-1582) on its own right.
public class CachePutListener implements PostInsertEventListener {
@Override
public void onPostInsert(final PostInsertEvent event) {
final EntityPersister persister = event.getPersister();
final EventSource session = event.getSession();
finalObject entity = event.getEntity();
finalObject[] state = event.getState();
finalObject version = Versioning.getVersion(state, persister);
finalboolean doCache =
persister.isIdentifierAssignedByInsert() && persister.hasCache() && !persister.isCacheInvalidationRequired()
&& session.getCacheMode().isPutEnabled();
if (doCache) {
final CacheKey ck =
new CacheKey(event.getId(), persister.getIdentifierType(), persister.getRootEntityName(), session
.getEntityMode(), session.getFactory());
final CacheEntry ce =
new CacheEntry(event.getState(), persister, persister.hasUninitializedLazyProperties(entity, session
.getEntityMode()), version, session, entity);
finalObject cacheEntry = persister.getCacheEntryStructure().structure(ce);
finalboolean put = persister.getCacheAccessStrategy().afterInsert(ck, cacheEntry, version);
if (put && session.getFactory().getStatistics().isStatisticsEnabled()) {
session.getFactory().getStatisticsImplementor().secondLevelCachePut(
persister.getCacheAccessStrategy().getRegion().getName());
} // if } // if }
}
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
Just implemented and tested a quick and dirty workaround with a custom PostInsertEventListener registered with the post-commit-insert event. This seems to work fine so far as the entity is now put in the second-level cache on insert.
I don't know what this breaks though, so of course I don't want to use this in production code (clearly there must be a reason why this isn't done in the hibernate core code and the according parts are commented out there, see also EntityIdentityInsertAction.doAfterTransactionCompletion()). Apart from this the post-commit-insert listener is also called for rolled-back transactions, which is another issue (HHH-1582) on its own right.