Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

EJB 3.0 design question

 
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:
<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
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic