Getting the entity id with JPA

View: New views
17 Messages — Rating Filter:   Alert me  

Getting the entity id with JPA

by Aleksei Valikov-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am looking for a generic way to retrieve an id of the entity which
was previously persisted with the given entity manager. It seems like
JPA does not provide such functionality - or am I missing something?

I need this to be really generic - I don't know exact entity classes,
I don't know if they follow any conventions about ids, I don't know if
ids are assigned or auto generated.

The best solution I come up with is the following:

* If the entity is annotated, check @Id and @EmbeddedId annotations
* Otherwise, try using proprietary APIs of known entity managers like Hibernate

I've attached what I've written so far. Is there a more standard way
to get an id of the entity?

Bye.
/lexi


EntityUtils.java (5K) Download Attachment

Re: Getting the entity id with JPA

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Aleksei,

There may be more than 1 @Id annotation if the entity defines @IdClass on it.

Regards,
-marina

Aleksei Valikov wrote:

> Hi,
>
> I am looking for a generic way to retrieve an id of the entity which
> was previously persisted with the given entity manager. It seems like
> JPA does not provide such functionality - or am I missing something?
>
> I need this to be really generic - I don't know exact entity classes,
> I don't know if they follow any conventions about ids, I don't know if
> ids are assigned or auto generated.
>
> The best solution I come up with is the following:
>
> * If the entity is annotated, check @Id and @EmbeddedId annotations
> * Otherwise, try using proprietary APIs of known entity managers like Hibernate
>
> I've attached what I've written so far. Is there a more standard way
> to get an id of the entity?
>
> Bye.
> /lexi


Re: Getting the entity id with JPA

by Aleksei Valikov-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi.

> There may be more than 1 @Id annotation if the entity defines @IdClass on
> it.

Yes, I know, the code I've posted does not pretend to be complete.

The question is if there is a standard way to get the id of the entity
(like, an oppositoe of getting the entity by its id). The trick with
annotations works, but what if entities are mapped using the XML
mappings?

Bye.
/lexi

RE: Getting the entity id with JPA

by djclarke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The following code was written for EclipseLink but should be compatible with TopLink Essentials with some minor changes.

Doug

import java.util.Vector;

import javax.persistence.EntityManager;

import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.internal.jpa.CMP3Policy;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.jpa.JpaEntityManager;
import org.eclipse.persistence.jpa.JpaHelper;

/**
 *
 * @author djclarke
 *
 */
public class PKHelper {

        /**
         *
         * @param entity
         * @return
         * @throws IllegalArgumentException
         *             if the class passed in is not an entity or the provided
         *             EntityManager is not an EclipseLink implementation.
         */
        public static Object extractPrimaryKey(EntityManager em, Object entity) {
                if (entity == null) {
                        return null;
                }

                JpaEntityManager emImpl = JpaHelper.getEntityManager(em);

                if (em == null) {
                        throw new IllegalArgumentException(
                                        "EntityManager is either not EclipseLink's or its a container wrapper returniong null from getDelegate()");
                }

                ClassDescriptor descriptor = emImpl.getSession().getClassDescriptor(
                                entity.getClass());

                if (descriptor == null || !descriptor.hasCMPPolicy()) {
                        throw new IllegalArgumentException("Entity: " + entity
                                        + " does not have a valid descriptor to extract the PK");
                }
               
                Vector pks = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(entity, (AbstractSession) emImpl.getSession());

                if (pks.size() == 1) {
                        return pks.get(0);
                }
               
                CMP3Policy policy = (CMP3Policy) descriptor.getCMPPolicy();
                return policy.createPrimaryKeyInstance(pks);
        }
}

-----Original Message-----
From: Aleksei Valikov [mailto:aleksei.valikov@...]
Sent: Saturday, July 26, 2008 7:13 AM
To: persistence@...
Subject: Re: Getting the entity id with JPA


Hi.

> There may be more than 1 @Id annotation if the entity defines @IdClass on
> it.

Yes, I know, the code I've posted does not pretend to be complete.

The question is if there is a standard way to get the id of the entity
(like, an oppositoe of getting the entity by its id). The trick with
annotations works, but what if entities are mapped using the XML
mappings?

Bye.
/lexi

Re: Getting the entity id with JPA

by Marina Vatkina :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I think JPA 2.0 might have a standard API for that.

regards,
-marina

Aleksei Valikov wrote:

> Hi.
>
>
>>There may be more than 1 @Id annotation if the entity defines @IdClass on
>>it.
>
>
> Yes, I know, the code I've posted does not pretend to be complete.
>
> The question is if there is a standard way to get the id of the entity
> (like, an oppositoe of getting the entity by its id). The trick with
> annotations works, but what if entities are mapped using the XML
> mappings?
>
> Bye.
> /lexi

EclipseLink like a Persistence Provider

by Daniel Benítez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I tryed to install EclipseLink with Netbeans IDE 6.0.1, but  Netbeans not
understand EclipseLink like a Persistence Provider, what I must to do? I
created a new Library with Library Manager but anything happens.

regards,

Daniel


Re: EclipseLink like a Persistence Provider

by Wouter van Reeven :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Sep 15, 2008 at 12:11:42PM +0200, Daniel Benítez wrote:
> I tryed to install EclipseLink with Netbeans IDE 6.0.1, but  Netbeans not
> understand EclipseLink like a Persistence Provider, what I must to do? I
> created a new Library with Library Manager but anything happens.

EclipseLink contains several subdirectories in the jlib dir. In order for
NetBeans to recognise EclipseLink as a Persistence Provider, you need to include
eclipselink.jar from the jlib dir and javax.persistence_1.0.0.jar from the
jlib/jpa dir. This is how I did it.


HTH, Wouter van Reeven

--

People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"

Re: EclipseLink like a Persistence Provider

by Daniel Benítez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, I included this two files in Netbeans. With Tools>Libraries I add a new
Library named EclipseLink and then I Add JAR/Folder in order to add this two
files.  But Netbeans recognise TopLink (default), Hibernate, KODO and
OpenJPA, and not EclipseLink.





--------------------------------------------------
From: "Wouter van Reeven" <wouter@...>
Sent: Monday, September 15, 2008 1:32 PM
To: <persistence@...>
Subject:  Re: EclipseLink like a Persistence Provider

> On Mon, Sep 15, 2008 at 12:11:42PM +0200, Daniel Benítez wrote:
>> I tryed to install EclipseLink with Netbeans IDE 6.0.1, but  Netbeans not
>> understand EclipseLink like a Persistence Provider, what I must to do? I
>> created a new Library with Library Manager but anything happens.
>
> EclipseLink contains several subdirectories in the jlib dir. In order for
> NetBeans to recognise EclipseLink as a Persistence Provider, you need to
> include
> eclipselink.jar from the jlib dir and javax.persistence_1.0.0.jar from the
> jlib/jpa dir. This is how I did it.
>
>
> HTH, Wouter van Reeven
>
> --
>
> People: "If she weighs the same as a Duck, she's made of wood!"
> Sir Bedevere: "And therefore...?"
>

Re: EclipseLink like a Persistence Provider

by Wouter van Reeven :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Sep 15, 2008 at 01:50:24PM +0200, Daniel Benítez wrote:
> Yes, I included this two files in Netbeans. With Tools>Libraries I add a
> new Library named EclipseLink and then I Add JAR/Folder in order to add
> this two files.  But Netbeans recognise TopLink (default), Hibernate, KODO
> and OpenJPA, and not EclipseLink.

I had to stop and restart NetBeans and then it worked. I assume you did that
too? Besides that I created an Enterprise Application with an EJB module and a
Web module. By the way, this is on NetBeans 6.1 and I think you are using 6.0.1,
right?


Greets, Wouter

--

People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"

Re: EclipseLink like a Persistence Provider

by Daniel Benítez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yes, I stop and restart NetBeans and my computer. Yes I'm using Netbeans
6.0.1.



--------------------------------------------------
From: "Wouter van Reeven" <wouter@...>
Sent: Monday, September 15, 2008 2:00 PM
To: <persistence@...>
Subject:  Re: EclipseLink like a Persistence Provider

> On Mon, Sep 15, 2008 at 01:50:24PM +0200, Daniel Benítez wrote:
>> Yes, I included this two files in Netbeans. With Tools>Libraries I add a
>> new Library named EclipseLink and then I Add JAR/Folder in order to add
>> this two files.  But Netbeans recognise TopLink (default), Hibernate,
>> KODO
>> and OpenJPA, and not EclipseLink.
>
> I had to stop and restart NetBeans and then it worked. I assume you did
> that
> too? Besides that I created an Enterprise Application with an EJB module
> and a
> Web module. By the way, this is on NetBeans 6.1 and I think you are using
> 6.0.1,
> right?
>
>
> Greets, Wouter
>
> --
>
> People: "If she weighs the same as a Duck, she's made of wood!"
> Sir Bedevere: "And therefore...?"
>

Re: EclipseLink like a Persistence Provider

by Daniel Benítez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everybody,

I can't see EclipseLink like a Persistence Provider in Netbeans.

With Netbeans I created a new Library named EclipseLink and add
eclipselink.jar and  javax.persistence_1.0.0.jar  files. Netbeans understand
the jar files but when I want to created a Persistence Unit, EclipseLink is
not a  Persistence Provider, and I can't created the Persistence Unit with
EclipseLink.

What can I do to created a new Persistence Provider?

Thanks.

Daniel Benítez.


--------------------------------------------------
From: "Daniel Benítez" <danibenitez@...>
Sent: Monday, September 15, 2008 4:12 PM
To: <persistence@...>
Subject:  Re: EclipseLink like a Persistence Provider

> Yes, I stop and restart NetBeans and my computer. Yes I'm using Netbeans
> 6.0.1.
>
>
>
> --------------------------------------------------
> From: "Wouter van Reeven" <wouter@...>
> Sent: Monday, September 15, 2008 2:00 PM
> To: <persistence@...>
> Subject:  Re: EclipseLink like a Persistence Provider
>
>> On Mon, Sep 15, 2008 at 01:50:24PM +0200, Daniel Benítez wrote:
>>> Yes, I included this two files in Netbeans. With Tools>Libraries I add a
>>> new Library named EclipseLink and then I Add JAR/Folder in order to add
>>> this two files.  But Netbeans recognise TopLink (default), Hibernate,
>>> KODO
>>> and OpenJPA, and not EclipseLink.
>>
>> I had to stop and restart NetBeans and then it worked. I assume you did
>> that
>> too? Besides that I created an Enterprise Application with an EJB module
>> and a
>> Web module. By the way, this is on NetBeans 6.1 and I think you are using
>> 6.0.1,
>> right?
>>
>>
>> Greets, Wouter
>>
>> --
>>
>> People: "If she weighs the same as a Duck, she's made of wood!"
>> Sir Bedevere: "And therefore...?"
>>
>

Re: EclipseLink like a Persistence Provider

by Andrei Badea :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel Benítez wrote:
> Hi,
>
> I tryed to install EclipseLink with Netbeans IDE 6.0.1, but  Netbeans
> not understand EclipseLink like a Persistence Provider, what I must to
> do? I created a new Library with Library Manager but anything happens.

Hi Daniel,

Unfortunately NB 6.0.1 does not detect EclipseLink as a persitence
provider. Basic support for EclipseLink in Java SE environments was
added in NetBeans 6.1, and Java EE environments will be supported in
NetBeans 6.5 AFAIK.

Andrei

Re: EclipseLink like a Persistence Provider

by Daniel Benítez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Andrei,

I must to create a Enterprise Aplication with Netbeans and EclipseLink, is
it possible now?

Daniel Benitez.

--------------------------------------------------
From: "Andrei Badea" <Andrei.Badea@...>
Sent: Tuesday, September 16, 2008 3:26 PM
To: <persistence@...>
Subject:  Re: EclipseLink like a Persistence Provider

> Daniel Benítez wrote:
>> Hi,
>>
>> I tryed to install EclipseLink with Netbeans IDE 6.0.1, but  Netbeans not
>> understand EclipseLink like a Persistence Provider, what I must to do? I
>> created a new Library with Library Manager but anything happens.
>
> Hi Daniel,
>
> Unfortunately NB 6.0.1 does not detect EclipseLink as a persitence
> provider. Basic support for EclipseLink in Java SE environments was added
> in NetBeans 6.1, and Java EE environments will be supported in NetBeans
> 6.5 AFAIK.
>
> Andrei
>

Re: EclipseLink like a Persistence Provider

by Andrei Badea :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel Benítez wrote:
> Hi Andrei,
>
> I must to create a Enterprise Aplication with Netbeans and EclipseLink,
> is it possible now?

AFAIK, it should be.

Andrei

imports with EclipseLink

by Daniel Benítez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Everybody,

 I'm migrating from Oracle TopLink to EclipseLink. Now I need to import
EclipseLink packages in a clase but Netbeans 6.1 tell me that "package
org.eclipse  does not exist", but I import a EclipseLink library with
eclipselink.jar and javax.persistence_1.0.0.jar into de project. What is the
problem?

thanks,

Daniel Benítez.



--------------------------------------------------
From: "Andrei Badea" <Andrei.Badea@...>
Sent: Tuesday, September 16, 2008 4:53 PM
To: <persistence@...>
Subject:  Re: EclipseLink like a Persistence Provider

> Daniel Benítez wrote:
>> Hi Andrei,
>>
>> I must to create a Enterprise Aplication with Netbeans and EclipseLink,
>> is it possible now?
>
> AFAIK, it should be.
>
> Andrei
>

Re: imports with EclipseLink

by Andrei Badea :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Daniel Benítez wrote:
> Hi Everybody,
>
> I'm migrating from Oracle TopLink to EclipseLink. Now I need to import
> EclipseLink packages in a clase but Netbeans 6.1 tell me that "package
> org.eclipse  does not exist", but I import a EclipseLink library with
> eclipselink.jar and javax.persistence_1.0.0.jar into de project. What is
> the problem?

I'm not sure. I tried your scenario in a daily build of 6.5 and I didn't
see any problems. If you can reproduce the problem with a clean userdir,
please file an issue against NetBeans.

Andrei

Re: imports with EclipseLink

by Daniel Benítez :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Andrei,

I tried with a clear project and the problem is the same. In fact,  I only
created  a "New Project" > "Java EE" > "Enterprise Application" with
standars parameters changing "Create web application module" to "Create
Application Client Module", because my application run as Application Client
Module.

Then, in project property I add a new Library named EclipseLink that I
created with eclipselink.jar and javax.persistence_1.0.0.jar files.

Then I have a new package named org.eclipse, but if I import this package,
Netbeans tell me: "package org does not exists". In fact I have the same
standars packages that starts with org like codehaus, apache, etc.

I tried with NetBeans 6.1 and 6.5, and have the same problem.



--------------------------------------------------
From: "Andrei Badea" <Andrei.Badea@...>
Sent: Thursday, September 18, 2008 10:19 AM
To: <persistence@...>
Subject:  Re: imports with EclipseLink

> Daniel Benítez wrote:
>> Hi Everybody,
>>
>> I'm migrating from Oracle TopLink to EclipseLink. Now I need to import
>> EclipseLink packages in a clase but Netbeans 6.1 tell me that "package
>> org.eclipse  does not exist", but I import a EclipseLink library with
>> eclipselink.jar and javax.persistence_1.0.0.jar into de project. What is
>> the problem?
>
> I'm not sure. I tried your scenario in a daily build of 6.5 and I didn't
> see any problems. If you can reproduce the problem with a clean userdir,
> please file an issue against NetBeans.
>
> Andrei
>
LightInTheBox - Buy quality products at wholesale price!