You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Himadri <hi...@yahoo.co.in> on 2010/08/31 06:44:49 UTC

Need id to set in VO .

Hi,

Am using EJB3.0 Entity on WL 10.3 . The application is designed in
ValueObject model i.e Entities are not directly passed to client but
converted into value objects and then sent to client. I cannot change this
design now. 
Value object also has primary key (id) column . My requirement is to create
an entity and send VO back with id of newly created entity set. 

Now I understand , id is not generated untill the entity is inserted into
database.. At the end of method when i convert Entity to VO , I donot get
the primay key as the transaction is not committed yet. 
So to overcome this problem , I have user em.flush() ... and then I could
get the key..  I understand its a bad practice to use flush .. but can
anyone suggest how else can I get the system generated primary key at the
end of method and set it back in VO ? 

Thanks
Himadri 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Need-id-to-set-in-VO-tp5481138p5481138.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Need id to set in VO .

Posted by Himadri <hi...@yahoo.co.in>.
hey ! Thanks. Lemme try this and will post my findings again.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Need-id-to-set-in-VO-tp5481138p5486473.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Need id to set in VO .

Posted by Jeremy Bauer <te...@gmail.com>.
Hi Himadri,

One option is to add a PostPersist lifecycle method to your entity class.
The id is guaranteed to be available during this callback.  During the
lifecycle method you can set the id on your value object.  The simplest (but
not pretty) way to do that might be to store a transient reference to the
value object within the entity.  Note: make sure to return that same ref
from your EJB method.  PostPersist will fire after commit, modifying the id
of the value object en route.

For example:

@Entity
public class MyEntity {

    transient public MyValue _value;

...

    @PostPersist
    public void postPersist() {
        _value.id = getId();
    }
}

hth,

-Jeremy

On Mon, Aug 30, 2010 at 11:44 PM, Himadri <hi...@yahoo.co.in> wrote:

>
> Hi,
>
> Am using EJB3.0 Entity on WL 10.3 . The application is designed in
> ValueObject model i.e Entities are not directly passed to client but
> converted into value objects and then sent to client. I cannot change this
> design now.
> Value object also has primary key (id) column . My requirement is to create
> an entity and send VO back with id of newly created entity set.
>
> Now I understand , id is not generated untill the entity is inserted into
> database.. At the end of method when i convert Entity to VO , I donot get
> the primay key as the transaction is not committed yet.
> So to overcome this problem , I have user em.flush() ... and then I could
> get the key..  I understand its a bad practice to use flush .. but can
> anyone suggest how else can I get the system generated primary key at the
> end of method and set it back in VO ?
>
> Thanks
> Himadri
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Need-id-to-set-in-VO-tp5481138p5481138.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>