English 中文(简体)
玩耍框架中使用元素集合时的 Lazy Indition 例外
原标题:LazyInitializationException when using ElementCollection in Play framework

我的一套应用软件中有一个用户实体,其定义如下:

public class User extends Model {

    private String name;

    private byte[] sk;

    @Column(columnDefinition = "BINARY(272)")
    private byte[] pk;

    private int port;

    @OneToOne
    public Profile profile;

    @ElementCollection
    public List<String> friends;

    @ElementCollection
        public List<String> mirrors;
...
}

在我的应用程序的不同部分( 控制器类) 使用一种方法, 我正在检索并试图修改镜像列表如下 :

    User u = User.connect(username);
    int port = ProfileFinder.getLocation(username, mirror);
    u.mirrors.remove(mirror);
    u.save();

这是在错误地指出:

LazyInitializationException occured : failed to lazily initialize a collection of role: models.User.mirrors, no session or session was closed

我怀疑这是因为我误解了 代码E元素集合 标签中的某些元素, 但是谁能澄清我怎样才能纠正这一点?

谢谢

最佳回答

默认情况下, XxxTo many 关联和元素收藏会被懒惰地装入。

这意味着只有在需要时才将收集元素从数据库中装入,当调用一种收集方法时。 当然, 实体需要附在届会之后才能工作。 如果会议闭幕, 您得到的例外将被丢弃 。

您要么通过设置批注的取回属性来急切地装入它,要么在返回收藏之前,在交易中使用一个查询或服务来初始化收藏,在交易中,在返回之前。请注意,如果您使收藏急切地装入,即使不需要收藏元素,它也会急切地装入ALWAYS。

问题回答

如果您不想从懒惰的负荷转向渴望,您还有另一种选择:合并。

User u = User.connect(username);
u.merge();

合并将取出一个与会话断开的天体并重新连接。

例如,如果您隐藏一个对象(在此情况下为用户实例),在未首先在对象上使用.merge () 的情况下,您无法获取用户对象的镜像。

类级用户交易

@ 元件

@ 交易

公共阶级班Name

是的,你应该使用 EEAGER 注释,但要小心,因为,正如JB Nizet 所说,这些元素总是会被热切地装入。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签