使用 EcliplseLink JPA 如何在持久化对象时禁用所有关系查找?
当我使用外键持久化一个对象时,我在日志中看到 eclipselink 关闭并执行验证查询,如下所示: 我还看到 eclipselink 找到了这个对象并绑定了它。我不希望它做这两件事。
基本上我试图保存对象A,并且A与对象B有关系。当我去插入一个新的A时,我所拥有的只是B的ID,而不是B类的实例,并且我对获取不感兴趣本例中是 B 的一个实例。
像这样的事情:
String b_id = "12345";
A a = new A();
B b = new B();
b.setId(b_id);
a.setB(b);
//begin tran
try {
em.persist(a);
//commit
} catch (Exception e) {
e.printStackTrace();
//rollback
}
这会在日志中导致这种情况:
Execute query DoesExistQuery(referenceClass=B)
SELECT ID FROM B WHERE (ID = ?)
bind => [12345]
现在我想插入 A,而 Eclipselink 不会验证是否存在具有此 Id 的 B,并且我不希望它将这个 B 绑定到 A 的字段,该字段是 。
我不希望 Eclipselink 将这种开销添加到插入中,因为数据库无论如何都会在插入之前执行此验证,并且在这种情况下我不需要 B 的实际实例
任何答复表示赞赏,谢谢!
When I persist an object with a foreign key, I see in the logs that eclipselink goes off and does a verification query, like this:
I also see that eclipselink finds this object and binds it. I don't want it to do either of these things.
Basically I am trying to save object A, and A has a relationship with object B. When I go to insert a new A, all I have is the ID for B, not an instance of class B, and I am not interested in obtaining an instance of B in this case.
Something like this:
String b_id = "12345";
A a = new A();
B b = new B();
b.setId(b_id);
a.setB(b);
//begin tran
try {
em.persist(a);
//commit
} catch (Exception e) {
e.printStackTrace();
//rollback
}
which causes this in the logs:
Execute query DoesExistQuery(referenceClass=B)
SELECT ID FROM B WHERE (ID = ?)
bind => [12345]
Now I want to insert A without Eclipselink going to verify if there exists a B with this Id, and I don't want it to bind this B to the field of A which is an instance of B.
I don't want Eclipselink adding this overhead to the insert, because the database is going to be doing this validation before the insert anyways, and I don't need an actual instance of B in this case.
Any reply is appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 getReference 可能是最好的。您还可以在描述符上自定义DoesExist策略(@ExistenceChecking(ASSUME_EXISTENCE))。
Using getReference is probably best. You could also customize your DoesExist policy on your descriptor (@ExistenceChecking(ASSUME_EXISTENCE)).
这通常是
EntityManager.getReference()
方法:它创建并向具有给定类和 ID 的实体返回代理,而无需访问数据库:我有没有经验虽然使用 EclipseLink,所以我不知道这个查询到底什么时候执行。
This is normally the role of the
EntityManager.getReference()
method: it creates and returns a proxy to the entity having the given class and ID, without hitting the database:I have no experience with EclipseLink though, so I don't know when exactly is this query executed.
A <-> 上的级联注释B关系决定了额外的调用。也许您有 CascadeType.ALL,而您想要的只是 CascadeType.REFRESH 和 CascadeType.REMOVE。
The cascade annotation on A <-> B relation determines that extra call. Probably you have CascadeType.ALL and what you want is CascadeType.REFRESH and CascadeType.REMOVE only.