File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB 3.0 design question" Watch "EJB 3.0 design question" New topic

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:
<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;



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.

I agree. Here's the link:
subject: EJB 3.0 design question
jQuery in Action, 3rd edition