users@glassfish.java.net

Re: Entity Instance Singleton

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Wed, 21 May 2008 14:27:30 -0700

3.1 EntityManager
An EntityManager instance is associated with a persistence context. A
persistence context is a set of entity instances in which for any persistent
entity identity there is a unique entity instance.

-----
You are probably not selecting just an 'id' in the native query, right?

regards,
-marina

Markus Karg wrote:
> I stumbled across the behaviour that when doing the following code in a
> transaction, I always get the same entity instance:
>
>
>
> MyEntity a = em.find(MyEntity.*class*, *new* MyEntityPK("xyz")));
>
> MyEntity b = em.createQuery("SELECT m FROM MyEntity m WHERE m.pk.id =
> 'xyz'").getSingleResult());
>
> MyEntity c = em.createNativeQuery("SELECT id FROM MyEntity WHERE id =
> 'xyz'").getSingleResult());
>
>
>
> a and b always point to the exact same object. I would understand that
> the objects have similar content. But really I get three times the same
> object, even after a native SQL query. Seems like the JPA Provider is
> checking the cache for existing objects before creating a new one.
>
>
>
> Now my question is: Is that behaviour enforced by the JPA specification,
> so can I rely on this beeing ensured by EACH JPA Provider?
>
>
>
> Thanks
>
> Markus
>
>
>
> QUIPSY QUALITY GmbH & Co. KG
>
> Ein Unternehmen der MES-Gruppe
>
> Stuttgarter Strasse 23
>
> D-75179 Pforzheim
>
> Tel: 07231-9189-52
>
> Fax: 07231-9189-59
>
> www.quipsy.de <http://www.quipsy.de>
>
> karg_at_quipsy.de <mailto:karg_at_quipsy.de>
>
> Registergericht Mannheim HRA 701214
>
> Geschäftsführer: Nils Schroeder
>
>
>
> Diese E-Mail enthält persönliche, vertrauliche und vor Weitergabe
> geschützte Informationen und ist ausschließlich für den vorgesehenen
> o.g. Empfänger (Adressaten) bestimmt. Falls Sie diese E-Mail
> versehentlich erhalten haben und nicht der vorgesehene Empfänger sind,
> bitten wir Sie, die E-Mail und deren Anhänge nicht aufzubewahren, nicht
> zu vervielfältigen, nicht zu nutzen und nicht weiterzugeben. Bitte
> informieren Sie uns als Absender über diesen Zustellungsfehler und
> löschen Sie die E-Mail.
>
>
>