| 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:
This error always happens after clearing the persistence context (either manually or when doing a commit) when the authority is reloaded:
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();
LOGGER.error("auth: {}", $session().get(Authority.class, a.getId()));
where
@Entity
public class Authority extends SidEntity<BOUser> {
(...)
}
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;
}
The issue happens in TwoPhaseLoad.doInitializeEntity, when the hydrated state of JafSid is resolved.
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. |