You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Craig L Russell <Cr...@Sun.COM> on 2006/12/13 03:33:57 UTC

Re:

Hi Dain,

On Dec 12, 2006, at 6:24 PM, Dain Sundstrom wrote:

> Does JPA guarantee that only one bean will be activated for a  
> specific pk per transaction?

Yes.

> Specifically, will OpenJPA ever create more that one bean for a  
> specific Class:PK in a single transaction?

No.
>
> The reason I'm asking is I may want to use an identity map and  
> don't want to end up with multiple entries for the same pk.

That is actually the internal implementation.

> This would be very convenient for managing beans that have not had  
> identity established yet.

Huh? JPA implementations only create beans that have a database  
identity already. For "new" beans, you need to call em.persist in  
order to create a database identity, and this will fail (eventually,  
according to both CMP2 and EJB3 specifications) if there is already a  
persistent bean with the same identity.

Craig
>
> -dain

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: Instances and transactions...

Posted by Craig L Russell <Cr...@Sun.COM>.
On Dec 12, 2006, at 6:48 PM, Dain Sundstrom wrote:

> Sorry about he subject free email :)
>
> On Dec 12, 2006, at 6:42 PM, Patrick Linskey wrote:
>
>>> On Dec 12, 2006, at 6:24 PM, Dain Sundstrom wrote:
>>>
>>>> Does JPA guarantee that only one bean will be activated for a
>>>> specific pk per transaction?
>>>
>>> Yes.
>>>
>>>> Specifically, will OpenJPA ever create more that one bean for a
>>>> specific Class:PK in a single transaction?
>>>
>>> No.
>>
>> The full story is a little bit more complicated with OpenJPA. JPA  
>> guarantees
>> that object equality (==) comparison must pass for two references  
>> that point
>> to the same database record in the same transaction. IOW, however you
>> navigate to / query for a given record, you'll get back a  
>> reference to the
>> same Java object in a given transaction.
>>
>> The complication is that in most circumstances, OpenJPA holds weak
>> references to all objects except ones with unflushed  
>> modifications. The
>> guarantee I outlined above still holds, since if you hold a  
>> reference to an
>> instance, our weak reference will be maintained as well, and  
>> equality tests
>> will pass. But if you release your reference, the instance may become
>> garbage-collected, so if you later query for / navigate to the  
>> instance, you
>> might end up with a different object reference.
>>
>> I bring all this up because it means that OpenJPA does not  
>> guarantee that
>> the return value of System.identityHashCode() will be the same for  
>> a given
>> record over the course of a transaction. So, be sure that you bear  
>> this
>> restriction in mind if you use an IdentityHashMap or some equivalent
>> structure.
>
> Very cool.

I maintain that you don't need to concern yourself with this issue.  
If you hold a strong reference to an instance, it won't ever be  
replaced by a different one with the same database identity. And if  
you hold a weak reference, you don't care because when you access it  
again, you will get the same instance as OpenJPA holds.

Craig
>
> -dain

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: RE: Instances and transactions...

Posted by Dain Sundstrom <da...@iq80.com>.
Sorry about he subject free email :)

On Dec 12, 2006, at 6:42 PM, Patrick Linskey wrote:

>> On Dec 12, 2006, at 6:24 PM, Dain Sundstrom wrote:
>>
>>> Does JPA guarantee that only one bean will be activated for a
>>> specific pk per transaction?
>>
>> Yes.
>>
>>> Specifically, will OpenJPA ever create more that one bean for a
>>> specific Class:PK in a single transaction?
>>
>> No.
>
> The full story is a little bit more complicated with OpenJPA. JPA  
> guarantees
> that object equality (==) comparison must pass for two references  
> that point
> to the same database record in the same transaction. IOW, however you
> navigate to / query for a given record, you'll get back a reference  
> to the
> same Java object in a given transaction.
>
> The complication is that in most circumstances, OpenJPA holds weak
> references to all objects except ones with unflushed modifications.  
> The
> guarantee I outlined above still holds, since if you hold a  
> reference to an
> instance, our weak reference will be maintained as well, and  
> equality tests
> will pass. But if you release your reference, the instance may become
> garbage-collected, so if you later query for / navigate to the  
> instance, you
> might end up with a different object reference.
>
> I bring all this up because it means that OpenJPA does not  
> guarantee that
> the return value of System.identityHashCode() will be the same for  
> a given
> record over the course of a transaction. So, be sure that you bear  
> this
> restriction in mind if you use an IdentityHashMap or some equivalent
> structure.

Very cool.

-dain

RE:

Posted by Patrick Linskey <pl...@bea.com>.
> On Dec 12, 2006, at 6:24 PM, Dain Sundstrom wrote:
> 
> > Does JPA guarantee that only one bean will be activated for a  
> > specific pk per transaction?
> 
> Yes.
> 
> > Specifically, will OpenJPA ever create more that one bean for a  
> > specific Class:PK in a single transaction?
> 
> No.

The full story is a little bit more complicated with OpenJPA. JPA guarantees
that object equality (==) comparison must pass for two references that point
to the same database record in the same transaction. IOW, however you
navigate to / query for a given record, you'll get back a reference to the
same Java object in a given transaction.

The complication is that in most circumstances, OpenJPA holds weak
references to all objects except ones with unflushed modifications. The
guarantee I outlined above still holds, since if you hold a reference to an
instance, our weak reference will be maintained as well, and equality tests
will pass. But if you release your reference, the instance may become
garbage-collected, so if you later query for / navigate to the instance, you
might end up with a different object reference.

I bring all this up because it means that OpenJPA does not guarantee that
the return value of System.identityHashCode() will be the same for a given
record over the course of a transaction. So, be sure that you bear this
restriction in mind if you use an IdentityHashMap or some equivalent
structure.

-Patrick
_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it.