LazyInitializationException when using ElementColl

2019-02-22 08:13发布

I have an User entity in my applications set of models that is defined as follows:

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;
...
}

and in a method in a different part of my application (a controller class) I am retrieving and attempting to modify the list of mirrors as follows:

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

This is throwing an error stating that:

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

I suspect this is due to me misunderstanding some element of the @ElementCollection tag, but can anyone clarify how I could rectify this?

Thanks.

3条回答
Lonely孤独者°
2楼-- · 2019-02-22 08:34

By default, XxxToMany associations and element collections are lazy loaded.

This means that the collection elements are loaded from the database only when needed, when one of the collection methods is called. But of course, the entity needs to be attached to its session for this to work. If the session is closed, the exception you got is thrown.

Either you make it eagerly loaded by setting the fetch attribute of the annotation, or you use a query or service that initialize the collection, in the transaction, before returning it. Beware that if you make it eagerly loaded, it will ALWAYS be eagerly loaded, even if you don't need the collection elements.

查看更多
男人必须洒脱
3楼-- · 2019-02-22 08:41

Yes, you should use EAGER annotation, but be careful because, as JB Nizet says, those elements will be always eagerly loaded.

查看更多
仙女界的扛把子
4楼-- · 2019-02-22 08:41

If you don't want to change from lazy load to eager, you have another option: merge.

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

Merge will take an object that is disconnected from the session and reconnect it.

For example, if you cache an object (in this case an instance of User) you cannot retrieve the user object's mirrors without first using .merge() on the object.

查看更多
登录 后发表回答