This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes EJB 3.0 design question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB 3.0 design question" Watch "EJB 3.0 design question" New topic
Author

EJB 3.0 design question

Raj Bhandari
Ranch Hand

Joined: Oct 19, 2005
Posts: 97
Hi everyone,

I have a design ques and I'm sure its very common.
Suppose I have a Entity which can contain a collection of other enitities. Eg:
<blockquote>code:
<pre name="code" class="core">
public class Person implements Serializable {

private List<UserRole> userRoles;

@OneToMany(cascade=CascadeType.ALL, mappedBy="person")
public List<UserRole> getUserRoles() {
return userRoles;
}

public void setUserRoles(List<UserRole> userRoles) {
this.userRoles = userRoles;
}

}

</pre>
</blockquote>

How should my SessionBean interface look for adding/updating a person?

public long addPerson(Person p);
public long updatePerson(Person p);

where p contains list of userroles within it so that when I do em.persist(p), a person record and user roles record are created/updated?
(Here I wonder.. suppose I have 3 userroles added in db. Now I remove one from the list and try to persist; will the container know that one was deleted or it will just go and update the 2 which are there on the list?)

Basically here, the container takes care of adding the userroles for me.

or should I do this instead?

public long addPerson(Person p, List userRolesToAdd, List userRolesToDelete)?

In this case above, I add extra methods addUserRole(UserRole userRole), DeleteUserRole to the Person entity bean class.

And always in my session bean, I iterate thro' both the lists and call the addUserRoel and DeleteUserRole in the loop.

I'm not sure which approach to take and why do examples in books show these extra methods when really the container takes care of it for us when we do em.persist(p).

I'd appreciate your thoughts.

Tks,
Raj
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3.0 design question
 
Similar Threads
HFSJ - Doubt
Telnet Disconnecting from Client app when executing query.
rendering each child t:tree node in seperate column in panel grid
Why does XSLT insert xmlns=""?
how to update OneToMany