Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB 3.0 design question  RSS feed

Raj Bhandari
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!