开发者

Can no persist entity with lazy loading elements

I have entities

@Entity public class A implements Serializable {
    @OneToMany(mappedBy = "a", fetch = FetchType.LAZY)
    private List<B> bList;
}

@Entity public class B implements Serializable{
    @JoinColumn(name = "A_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private A a;

    @JoinColumn(name = "C_ID", referencedColumnName = "ID", nullable = false)
    @OneToOne(cascade = CascadeType.ALL, optional = false,开发者_JAVA技巧 fetch = FetchType.EAGER)
    private C c;
}

@Entity public class C implements Serializable {
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "c", fetch = FetchType.EAGER)
    private B b;
}

if I pass B entity to ejb method from remote client, then entity passed to app server is NULL; If I change LAZY loading to EAGER, then every thing goes well. I do not want to use EAGER loading, how to avoid EAGER loading?


The only way you can persist instance of B type, received from remote client is restore state of its children from old copy and then persist.

public B updateFromRemote(B b) {
   B oldCopy = bDao.findBById(b.getId());
   b.setA(oldCopy.getA());
   b.setC(oldCopy.getC());
   return bDao.save(b);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜