persistence@glassfish.java.net

Re: Simple foreign keys

From: <bht_at_actrix.gen.nz>
Date: Tue, 08 Dec 2009 11:06:03 +1300

Hi Gordon,

Many thanks, this works for me.

However, in one case where I applied this I am seeing bad performance
due to unexpected repeated fetching of the same entities with the same
IDs. In that case I use EntityManager only to keep its data fresh
otherwise I would write raw JDBC. It is ironic that while trying to
maintain the cache that is supposed to be the the source of
performance gains, I am using an inefficent process that gets even
more inefficient because it does not take advantage of the cache. I am
confused by this rather simple case which I reported at:


https://bugs.eclipse.org/bugs/show_bug.cgi?id=297127
"Unnecessary fetching causes bad performance"

This is either a real bug, or I need to learn more about how to use
JPA effectively. What do you think?

Many thanks

On Mon, 07 Dec 2009 09:07:08 -0400, you wrote:

>The easiest mechanism is to map this field as a @Basic but ensure you
>also use @Column and set 'updatable=false, insertable=false'.
>Alternatively you can create a JPQL query that returns the ID directly.
>--Gordon
>
>bht_at_actrix.gen.nz wrote:
>> Hi,
>>
>> Is there support for reading the simple foreign key field value of a
>> @ManyToOne related entity as it is stored in the database, without
>> forcing EntityManager to fetch the related entity?
>>
>> I need this when I process an ordered result list in groups of the
>> related parent entity. Unfortunately, caching does not work in this
>> case, and EntityManager re-fetches the parent entity on every call
>> child.getParent().getId(). I only need the ID to detect change of
>> group.
>>
>> I know I can completely remove the relationship and work exclusively
>> with an ID field. But then I lose all foreign key constraints
>> generated by TopLink DDL.
>>
>> Before I take this option I would like to know whether there is any
>> other best practice for this type of scenario.
>>
>> I have tried to use BOTH
>>
>> @Column(name="parent_id")
>> private Integer parentId;
>>
>> AND
>>
>> @ManyToOne
>> @JoinColumn(name="parent_id", nullable = true, insertable=false,
>> updatable=false)
>> private Category parent;
>>
>> with the same coumn name, but then I get exceptions, one of them
>> unexplainable (cannot find parent that was inserted before), so I am
>> not sure whether I am dealing with a TopLink bug.
>>
>> Many thanks
>>
>> Bernard
>>
>>