users@glassfish.java.net

Re: Entity beans, persistence and CascadeType.

From: Ryan J <ryan.j_at_vacode.com>
Date: Thu, 28 Jun 2007 21:08:01 -0600

Thanks for the reply. After reading a few replies and realizing that most
consisted of 'use the built in mechanism' or 'the id is automatically updated' I
figured I was overlooking something.

After a fairly decent amount of time surfing the net, I realized things can be a
lot easier than what I've made them. I didn't realize it before, but the
application client container provides functionality similar to the server in
terms of injecting a @PersistenceUnit and @EntityManager.

Using the ACC I can simply inject an @EntityManager and use it as if I were
working server side; no need to detach / re-attach / set ids / etc...

A lot of the 'application client' EJB examples I've seen do what I was trying;
create a Remote interface that can be used as a facade to retrieve / persist
data. Luckily I stumbled across a blog by Sahoo (by fluke) or I still wouldn't
know the ACC provides this (wonderful) functionality.

Ryan

glassfish_at_javadesktop.org wrote:
> Well, you can still certainly do that.
>
> But, you should still use the mechanism provided by the JPA to autoassign the primary key to the children, rather than being explicit.
>
> Then, you could simply to this:
>
> [code]
> int authorId = sb.create(author);
> author.setId(authorId);
> author.updateChildIds(authorId);
>
> public class Author {
> ...
> public void updateChildIds(int authorId) {
> for(Book b : books) {
> b.setAuthorId(authorId);
> }
> }
> [/code]
>
> There's no reason to bring back the entire detached object if you don't want to and are willing to maintain it yourself. But it's a lot easier to let the EnityManager handle it on the server side.
>
> Note, you will have to do some management of the relationship on the server side no matter what (specifically merging changes to the Books collection with what the DB sees).
> [Message sent by forum member 'whartung' (whartung)]
>
> http://forums.java.net/jive/thread.jspa?messageID=224613
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>