You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Roger <ro...@googlemail.com> on 2009/06/08 14:06:16 UTC

[NEWBIE] Auto generated key values

Hi 

I'm writing my first application using the javax.persistence framework with 
openJPA as it's implementation over a MySQL database (5.1.34) I've managed to 
get everything setup and I'm able to persist my entities to the database 
successfully.

I'm using the
   @Id
    @GeneratedValue(strategy = GenerationType.AUTO)

to generate the primary keys for most of the database tables and this is where 
I'm running into problems. I need to know the value of the generated keys of
some of the entities so I can maintain foreign key relationships. I've found 
the bit in the manual that tells me to use OpenJPAEntityManager.getObject() 
method and this works, but it means that I have to cast my 
javax.persistence.EntityManager to an  OpenJPAEntityManager which means that 
my application classes are tied to the OpenJPA implementation. What am I 
missing?

Regards 

Re: [NEWBIE] Auto generated key values

Posted by Rick Curtis <cu...@gmail.com>.
Roger -
Can you just add a getId() method to your Entity and call that after you
persist your Entity?

-Rick

On Mon, Jun 8, 2009 at 7:06 AM, Roger <ro...@googlemail.com> wrote:

> Hi
>
> I'm writing my first application using the javax.persistence framework with
> openJPA as it's implementation over a MySQL database (5.1.34) I've managed
> to
> get everything setup and I'm able to persist my entities to the database
> successfully.
>
> I'm using the
>   @Id
>    @GeneratedValue(strategy = GenerationType.AUTO)
>
> to generate the primary keys for most of the database tables and this is
> where
> I'm running into problems. I need to know the value of the generated keys
> of
> some of the entities so I can maintain foreign key relationships. I've
> found
> the bit in the manual that tells me to use OpenJPAEntityManager.getObject()
> method and this works, but it means that I have to cast my
> javax.persistence.EntityManager to an  OpenJPAEntityManager which means
> that
> my application classes are tied to the OpenJPA implementation. What am I
> missing?
>
> Regards
>