Hi there,
using bytecode enhancement with enableDirtyTracking=true and enabled second level cache, I'm encountering lazy init exceptions when the enhanced codes tries to clear the dirty attributes:
{noformat} org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:582) at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:574) at org.hibernate.collection.internal.AbstractPersistentCollection.access$200(AbstractPersistentCollection.java:55) at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:165) at org.hibernate.collection.internal.AbstractPersistentCollection$1.doWork(AbstractPersistentCollection.java:146) at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:247) at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:145) at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:143) at com.sobis.jaf.model.security.sid.JafSid.$$_hibernate_clearDirtyCollectionNames(JafSid.java) at com.sobis.jaf.model.security.sid.JafSid.$$_hibernate_clearDirtyAttributes(JafSid.java) at org.hibernate.tuple.entity.PojoEntityTuplizer.afterInitialize(PojoEntityTuplizer.java:297) at org.hibernate.persister.entity.AbstractEntityPersister.afterInitialize(AbstractEntityPersister.java:4659) at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:284) at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:128) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1152) at org.hibernate.loader.Loader.processResultSet(Loader.java:1011) at org.hibernate.loader.Loader.doQuery(Loader.java:949) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341) at org.hibernate.loader.Loader.doList(Loader.java:2692) at org.hibernate.loader.Loader.doList(Loader.java:2675) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507) at org.hibernate.loader.Loader.list(Loader.java:2502) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1897) at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:370) at com.sobis.jaf.tests.setup.DatabasePopulation.startBasicSetup(DatabasePopulation.java:122){noformat}
This error always happens after clearing the persistence context (either manually or when doing a commit) when the authority is reloaded: {code:java} Authority a = new Authority(); JafSid sid = new JafSid().setSid("sid!"); sid.setRelatedEntity(a); sid.setClassType("type!"); a.setClassType("type"); a.setSid(sid); a.setAuthority("name"); $session().save(a); $session().flush(); $session().clear(); //throws exception in next line! LOGGER.error("auth: {}", $session().get(Authority.class, a.getId())); {code}
where {code:java} @Entity public class Authority extends SidEntity<BOUser> {
(...) } {code} where SidEntity is {code:java} @Entity @Table(name = SidEntity.TABLE) public abstract class SidEntity<BO extends BOSidEntity<? extends SidEntity<BO>>> extends Base<BO> { (...) private JafSid sid; (...) @OneToOne(mappedBy = JafSid.RELATED_ENTITY, optional = false, fetch = FetchType.EAGER, orphanRemoval = true) @IndexedEmbedded(depth = 1, prefix = SID + JSONFieldDefinition.MODEL_SUFFIX) @Cascade(CascadeType.ALL) public JafSid getSid() { return sid; } (...) } where JafSid is
{code:java} public class JafSid extends Base<BOJafSid> implements Sid { (...) private Set<UserGroup> groups = new LinkedHashSet<>();
(...)
@IndexedEmbedded(depth = 1, prefix = GROUPS + JSONFieldDefinition.MODEL_SUFFIX) @ManyToMany(mappedBy = UserGroup.MEMBERS) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public Set<UserGroup> getGroups() { return groups; } {code}
The issue happens in TwoPhaseLoad.doInitializeEntity, when the hydrated state of JafSid is resolved.
!image-2018-03-22-16-47-49-937.png|thumbnail!
The collection that needs to be taken from this.xrefLoadingCollectionEntries throws the LazyInitializationException.
Maybe this already gives enough clues. As the inheritance structure of the entities is quite difficult, I wasn't able to prepare an isolated test case yet, but maybe this already helps to fix the issue. |
|