EclipseLink合并意外的级联 [英] EclipseLink merge unexpected cascade

查看:52
本文介绍了EclipseLink合并意外的级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体类user和device.

I have two entity classes user and device.

用户实体:

public class User {
    private Long userId;

    @OneToMany( mappedBy = "userId", fetch = FetchType.LAZY)
    private Collection<Device> deviceCollection;

和设备实体:

public class Device implements Serializable {
    @JoinColumn(name = "user_id", referencedColumnName = "user_id")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private User userId;

删除父用户后,将先前分离的设备实体合并到实体管理器中时,(先前删除的)用户和设备都重新插入数据库中.用户或设备实体上没有级联注释;因此,我不希望用户实体被重新插入,但是确实如此;

When I merge a previously detached device entity into the entity manager after the parent user has been deleted, both the (previously removed) user and the device are re-inserted into the database. There is no cascade annotation on user or device entity; therefore, I don't expect the user entity to be reinserted but it did;

如何防止合并操作级联到用户实体?

How do I prevent the merge operation to cascade to the user entity?

谢谢.

推荐答案

您应该使用版本号来防止实体被错误地重新使用.这将导致一个异常,因为规范并不确定在合并未标记为层叠全部或合并的关系时应如何处理.规范指出,受管实体将同步到数据库,而处理合并的部分暗示即使没有级联合并/所有选项的关系所引用的实体也将在之后进行管理.此行为可能不是预期的,但在澄清之前不应该依赖此行为.

You should be using a version number to prevent entities from being mistakenly resurected. This will force an exception, where as the specification is a bit unclear on what should happen when merging over a relation that isn't marked cascade all or merge. The spec states that managed entities will be synchronized to the database, while the section dealing with merge implies that even entities referenced by relations without the cascade merge/all options will be managed afterward. This behavior is probably not what was intended, but shouldn't be relied on until clarified.

这篇关于EclipseLink合并意外的级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆