Hibernate LazyInitializationException - change from Spring 3.0.0 to 3.0.1?
I'm a bit green with both Spring and Hibernate, so please bear with me.
We have recently taken over a web app that makes extensive use of Spring and Hibernate, and we have to make a number of changes. As we received it, it was setup with Spring 3.0.0.GA and Hibernate 3.3.1.GA. We need to utilize mvc:resources for static content, so we need to be using at least Spring 3.0.4. Unfortunately, some other code breaks; through trial and error I've found that the offending code works in 3.0.0 but breaks in 3.0.1:
Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.iir.sort.model.Offender.offenderAliases, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
at com.iir.sort.model.Offender.getFilteredOffenderAliases(Offender.java:822)
I've seen tons of references to this error, but I'm perplexed because it works fine in 3.0.0. Is there a known change post-3.0.0 that would affect this? FYI, I've also tried 3.0.4.GA, 3.1.0.RC1, 3.1.0.GA and 3.1.2.GA; I'm having the same issue in all of those. The *only* changes are to the JARs.
I'm not sure where to start, but here is some of the relevant code:
Offender.java, lines 811-832:
Code:
@OneToMany(mappedBy = "offender", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Set<OffenderAliases> getOffenderAliases() {
if (offenderAliases == null) {
offenderAliases = new HashSet<OffenderAliases>();
}
return offenderAliases;
}
@Transient
public Set<OffenderAliases> getFilteredOffenderAliases() {
Set<OffenderAliases> returnSet = new HashSet<OffenderAliases>();
for (OffenderAliases item : getOffenderAliases()) {
if (item.getEntityChangeSet() != null && item.getEntityChangeSet().getId() != null) {
if (item.getEntityChangeSet().getEntityChangeType().getId() != EntityChangeType.DELETE) {
returnSet.add(item);
}
} else {
returnSet.add(item);
}
}
return returnSet;
}
The fact that this works just fine with Spring 3.0.0 has me scratching my head. Any ideas?