使用EcliplseLink JPA,如何在保留对象时禁用所有关系查找? [英] Using EcliplseLink JPA how can I disable all the relationship lookups when persisting an object?

查看:79
本文介绍了使用EcliplseLink JPA,如何在保留对象时禁用所有关系查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用外键持久化对象时,我在日志中看到eclipselink关闭并执行验证查询,如下所示: 我还看到eclipselink找到了这个对象并绑定了它.我不希望它做任何一件事情.

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.

基本上,我试图保存对象A,并且A与对象B有关系.当我插入一个新的A时,我所拥有的只是B的ID,而不是类B的实例,并且我不是在这种情况下有兴趣获得B的实例.

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.

类似这样的东西:

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]

现在,我想插入一个不带Eclipselink的A来验证是否存在带有此ID的B,并且我不希望它将此B绑定到作为B实例的A字段上.

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.

我不希望Eclipselink将此开销添加到插入中,因为无论如何,数据库将在插入之前进行此验证,并且在这种情况下,我不需要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!

推荐答案

这通常是

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:

A a = new A();
B b = em.getReference(B.class, bId);
a.setB(b);

尽管我没有使用EclipseLink的经验,所以我不知道该查询何时确切执行.

I have no experience with EclipseLink though, so I don't know when exactly is this query executed.

这篇关于使用EcliplseLink JPA,如何在保留对象时禁用所有关系查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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