【问题标题】:roo entity having set of enums gives lazyloading exception具有一组枚举的 roo 实体给出延迟加载异常
【发布时间】:2013-12-03 19:53:05
【问题描述】:
//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;
}

如果我打电话

myEntity.isMyEnumPartOfMyEntity(MyEnumType.HELLO)

我得到休眠延迟加载异常

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

问题是如何快速加载我的 myEnumTypes 集?

谢谢。

【问题讨论】:

    标签: java spring hibernate persistence spring-roo


    【解决方案1】:

    让收藏热切:

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

    【讨论】:

    • 成功了。我不知道您可以在 ElementCollection 注释中指定 fetch 属性。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2011-03-14
    相关资源
    最近更新 更多