Question

//MyEntity.java

@RooJavaBean
@RooToString
@RooEntity
public class MyEntity {

    @PersistenceContext(unitName = "persistenceUnit")
    transient EntityManager entityManager;

    @NotNull
    private String name;

    @ElementCollection(targetClass = MyEnumType.class)
    @Enumerated(EnumType.STRING)
    private Set<MyEnumType> myEnumTypes = new HashSet<MyEnumType>();

    public boolean isMyEnumPartOfMyEntity(MyEnumType e) {
        for (MyEnumType type : myEnumTypes) {
            if (type.equals(e)) {
                return true;
            }
        }
        return false;
    }
}

//MyEnumType.java

public enum MyEnumType {
    HI, HELLO, GREETINGS;
}

If I call

myEntity.isMyEnumPartOfMyEntity(MyEnumType.HELLO)

I get hibernate lazyloading exception

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.rishi.myEnumTypes, no session or session was closed

The question is how do I eager load my myEnumTypes set?

Thanks.

Was it helpful?

Solution

Make the collection eager:

@ElementCollection(targetClass = MyEnumType.class, fetch = FetchType.EAGER)
@Enumerated(EnumType.STRING)
private Set<MyEnumType> myEnumTypes = new HashSet<MyEnumType>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top