不能使用延迟加载元素持久化实体

时间:2010-12-03 17:43:03

标签: jpa

我有实体

@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, fetch = FetchType.EAGER)
    private C c;
}

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

如果我从远程客户端传递B实体到ejb方法,那么实体 传递到app服务器是NULL;如果我将LAZY加载更改为EAGER,那么每件事情都会顺利进行。 我不想使用EAGER加载,如何避免EAGER加载?

1 个答案:

答案 0 :(得分:0)

从远程客户端接收的唯一可以持久存储B类型实例的方法是从旧副本恢复其子级的状态,然后保留。

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