persistence@glassfish.java.net

Re: Getting the entity id with JPA

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Thu, 24 Jul 2008 14:49:47 -0700

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