persistence@glassfish.java.net

Re: caching bug?

From: Jeffrey Blattman <jeffrey.blattman_at_gmail.com>
Date: Fri, 10 Aug 2007 15:44:18 -0700

hi marina,

Marina Vatkina wrote:
> Hi Jeffrey,
>
> Jeffrey Blattman wrote:
>> hi marina,
>>
>> i can't believe this can be so confusing. to remove a C, am i
>> supposed to do ...
>>
>> 1.
>>
>> A a = em.find(A.class, ...);
>> a.getCs().remove(c);
>
> Yes.
this works correctly, until i do an em.remove(a) in a latter
transaction. then i get a foreign key constraint violation.
>>
>> or can i only do:
>>
>> 2.
>>
>> C c = em.find(C.class, ...);
>> em.remove(c);
>
> You can if you then refresh 'a' or disable the cache, and C holds the
> FK to A.
>
when do i refresh A? the next time i access it? that's just
circumventing the cache right?

either

1. the cache cannot be used with this sort of containment relationship or
2. contained objects cannot be accessed directly as it will corrupt the
cache

is that correct?
>>
>> ? you are saying that if i do #2, i have to manually fix the
>> relationship by finding the A that contains the C and removing it
>> from the collection? if that's true, then obviously #2 is not the
>> correct thing to do ... ?
>>
>> i am trying to do #1, which appears to work, but when i do an
>> em.remove() on the A to clean up after the test, i get foreign key
>> violation.
>
> That's strange... you need to fix 'a' in the same transaction. You
> can also do em.refresh(a), but if you already removed 'c', how would
> you find the 'a'?
what do you mean by "fix"? the find() and getCs().remove(...) operations
are in the same transaction ... ? the removal of the A that causes the
FK violation is in a separate transaction. at time X i want to remove a
C from an A. and time Y i want to remove the entire A. that is the use case.

when would i do a refresh()? the next time i access the A? or in the
same transaction?

A contains a collection of Cs. what does a particular element in the
collection of Cs being removed have to do with finding the A?