复杂的JPA持久化订单问题
我正在开发一个具有一些不寻常的实体关系的项目,我在使用 JPA 时遇到了持续存在的问题。有两个相关的对象;用户,让我们称之为另一个X。用户与X有一对多和两个一对一的关系。它基本上看起来像这样
[用户实体]
@OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemoval=true)
private List<X> xList;
@OneToOne
@JoinColumn(name = "active_x1_id")
private X activeX1;
@OneToOne
@JoinColumn(name = "active_x2_id")
private X activeX2;
[X实体]
@ManyToOne()
@JoinColumn(name="user_id")
private User user;
当持久化一个新用户时,我也想在单个事务中保留两个 x 实体(一个用于 activeX1,一个用于 activeX2)。 Jpa 处理这个有点奇怪,日志看起来像这样:
INSERT INTO X VALUES (...) // x1
INSERT INTO USERS VALUES (...)
INSERT INTO X() VALUES (...) // x2
UPDATE USERS SET ...
UPDATE X VALUES (...) // updates x1
这使得不可能在数据库中使用 NOT NULL 约束。有没有更好的方法来处理这些多重关系?或者控制 JPA 持久化对象的顺序的方法? JPA 似乎确实明确地试图在这次操作中对我不利。任何帮助将不胜感激。
I'm working on a project with some unusual entity relations which i'm having problems persisting with JPA. There are two relevant objects; User and let's call the other X. User has a one-to-many AND two one-to-one relations to X. It basicly looks like this
[User entity]
@OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemoval=true)
private List<X> xList;
@OneToOne
@JoinColumn(name = "active_x1_id")
private X activeX1;
@OneToOne
@JoinColumn(name = "active_x2_id")
private X activeX2;
[X entity]
@ManyToOne()
@JoinColumn(name="user_id")
private User user;
When persisting a new user, I also want to persist two x entities (one for activeX1 and one for activeX2) in a single transaction. Jpa handles this abit weird, the log looks like this:
INSERT INTO X VALUES (...) // x1
INSERT INTO USERS VALUES (...)
INSERT INTO X() VALUES (...) // x2
UPDATE USERS SET ...
UPDATE X VALUES (...) // updates x1
This makes it impossible to use NOT NULL constraints in the database. Is there any better way to handle these multiple relationships? Or a way to control which order JPA persists objects? JPA really seems to explicitly try to work against me in this operation. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为什么不直接使用@NotNull注释呢?我认为没有办法改变持久顺序。你必须手动完成。像这样的事情,
Why don't you just use @NotNull annotation? I don't think there is a way to change persist order. You have to do it manually. Something like this,