1

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!

jhnclvr
  • 9,137
  • 5
  • 50
  • 55

3 Answers3

3

Using getReference is probably best. You could also customize your DoesExist policy on your descriptor (@ExistenceChecking(ASSUME_EXISTENCE)).

James
  • 17,965
  • 11
  • 91
  • 146
2

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);

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

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • That seemed to do the trick! I still see the select and the bind in the log but I do not see "Execute query DoesExistQuery" any longer, so I assume we are in the clear. Thanks! – jhnclvr Feb 22 '12 at 19:29
1

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.

Bogdan
  • 934
  • 7
  • 13