roo entity having set of enums gives lazyloading exception

518 views Asked by At
//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.

1

There are 1 answers

1
Taylor On BEST ANSWER

Make the collection eager:

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