Trying to persist an entity which contains a member variable that is a reference to some other entity which is not under the current persistence context will not be possible, when this happens one needs to fetch the required entity and set it in the original entity in order to respect the relationship and be allowed to persist it.
When I need to accomplish this I usually make use of the EntityManager’s find method, but that will hit the database and fetch the entire entity along with it’s relationships that may not be annotated for lazy loading. I was happy to find out about getReference, which suposedly won’t hit the database but return a proxy representation where only the primary key is available and that is really all that is required for this type of situation.
Unfortunately after some debugging I find myself being able to view all the information about the getReference‘d entity and not just the primary key when I “inspect” it via Eclipse debug mode.
Am I missing something? Am I being deceived by the debug mode? Could it be fetching the information like a getter method would when used on the proxy reference?
Thanks in advance
Whe you inspect it using the Eclipse debugger, the debugger initializes the proxy. Just turn on SQL logging, execute the
em.getReference()method, and verify that no select statement has been executed by your JPA engine.