users@glassfish.java.net

Re: Entity beans, persistence and CascadeType.

From: Ryan J <ryan.j_at_vacode.com>
Date: Thu, 28 Jun 2007 11:24:57 -0600

What I mean by "update the newly generated ids for the detached Entity beans" is:

I am creating the Entity in an application client, detached from the server. If I create an author with 3 books they
all start with an id of -1. When I persist the author using a remote interface it is sent across the network and the
actual persistence occurs on the server. If I use CascadeType.PERSIST all of the ids on the server side are updated
automatically, but the ids in the application client are still set to -1.

The only thing I can think of right now is to detach the updated author from the session and return it to the client as
a new Entity, but that seems like a lot of extra overhead just to update ids on the client side.

Right now the implementation of my remote interface used for saving would look something like this:

@PersistenceContext
private EntityManager em;

public int create(Author author) {
        em.persist(author);
        return(author.getId());
}

I could change it to something like this:

public Author create(Author author) {
        em.persist(author);
        return(author);
}

I would prefer to create each Entity individually so I can return the newly assigned id to the application client, but I
haven't found a way of doing so without getting the exception I mentioned in my original post.

Thanks again for your reply,
Ryan

Markus Fuchs wrote:
> Hi Ryan,
>
> What do you mean by "update the newly generated ids for the detached
> Entity beans"? Id's should never be modified after once set.
>
> Anyway, you might want to set CascadeType.ALL for this relationship.
> This way any new Books will be persisted when calling
> EntityManager.merge(anAuthor). Merge exchanges all books with managed
> copies in the book collection.
>
> -- markus.
>
> Ryan J wrote:
>> Thanks for the reply Markus.
>>
>> That works fine on the server side. In my case I'm using an application client and end up calling EntityManager.merge()
>> via a remote interface. My application client is not on the same machine as the server and I don't know of an easy way
>> to determine and update the newly generated ids for the detached Entity beans that are on the client if I use
>> CascadeType.PERSIST.
>>
>> Ryan
>>
>> Markus Fuchs wrote:
>>
>>> Hi Ryan,
>>>
>>> the Ids for both Author and Book are generated automatically by the
>>> Persistence Provider, as you specified SEQUENCE Id generation. Just
>>> specify CascadeType.PERSIST on the Author side of the relationship.
>>>
>>> Thanks,
>>>
>>> -- markus.
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>>
> --------------------------------------------------------------------- To
> unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net For
> additional commands, e-mail: users-help_at_glassfish.dev.java.net