persistence@glassfish.java.net

Getting the entity id with JPA

From: Aleksei Valikov <aleksei.valikov_at_gmail.com>
Date: Thu, 17 Jul 2008 09:55:05 +0200

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