[jboss-user] [JBoss Seam] - Re: Entities with one/many_to_many relationships and SEAM
dbatcn
do-not-reply at jboss.com
Mon Jul 24 13:03:25 EDT 2006
I hope this is not too off-topic, but I'm having some difficulty getting my Seam app up with @ManyToMany (though I suspect it's not really a Seam problem).
I've got two entity beans, User and Group, with a many-to-many relationship where User is the owner. I have a GroupManagerBean class which is a stateful session bean where I'd like to have a create() method that will create a new group with an initial user. I'm trying to figure out exactly how to annotate the classes and do the data update; my first few attempts haven't worked and I was hoping somebody could offer some advice about how to get this right: cascade types, idioms for object creation and update, etc.
I've tried to extract the relevant code snippets below. Pointers on how to do this or to previous discussions of this topic gratefully accepted.
Thanks.
User.java
| @Entity
| @Name("user")
| @Role(name="currentUser", scope=SESSION)
| @Table(name="USERS")
| public class User implements Serializable {
| ...
| private Set<Group> groups = new HashSet<Group>();
| @ManyToMany(cascade=CascadeType.ALL)
| @JoinTable(name="USER_GROUP",
| joinColumns={@JoinColumn(name="USER_ID")},
| inverseJoinColumns={@JoinColumn(name="GROUP_ID")})
| public Set<Group> getGroups() {
| return groups;
| }
| public void setGroups( Set<Group> groups ) {
| this.groups = groups;
| }
| }
|
Group.java
| @Entity
| @Name("group")
| @Table(name="GROUPS")
| public class Group implements Serializable {
| ...
| @ManyToMany(cascade=CascadeType.ALL)
| @JoinTable(name="USER_GROUP",
| joinColumns={@JoinColumn(name="USER_ID")},
| inverseJoinColumns={@JoinColumn(name="GROUP_ID")})
| public Set<Group> getGroups() {
| return groups;
| }
| public void setGroups( Set<Group> groups ) {
| this.groups = groups;
| }
| }
|
GroupManagerBean.java
| @Stateful
| @Name("groupManager")
| @LoggedIn
| public class GroupManagerBean implements GroupManager, Serializable {
| ...
| public void create() {
| group = new Group();
| group.setGroupname( newGroupname() );
| orgmobDatabase.persist( group );
| // User owns the bidirectional many-to-many relationship with Group
| user.getGroups().add( group );
| orgmobDatabase.merge( user );
| orgmobDatabase.refresh( group );
| // PROBLEM:
| // I thought that group.getUsers() would contain user, but it doesn't!
| //debug
| log.debug( "for user "+user+" created group: "+group);
| log.debug( "user "+user+" has "+user.getGroups().size()+" groups");
| log.debug( "group "+group+" has "+group.getUsers().size()+" users");
| for ( Group g : user.getGroups() ) {
| log.debug( "a group for user "+user+": "+g);
| }
| for ( User u : group.getUsers() ) {
| log.debug( "a user for group"+group+": "+u);
| }
| }
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3960467#3960467
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3960467
More information about the jboss-user
mailing list