users@glassfish.java.net

Re: Entity beans, persistence and CascadeType.

From: <glassfish_at_javadesktop.org>
Date: Thu, 28 Jun 2007 18:14:05 PDT

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