Is one to many bidirectional working?
john_anderson_ii May 18, 2004 9:53 PMI'm grinding my way though the IBM CMP tutorial, and converting the concepts to JBoss for practice. I am backending my application with MySQL.
Everything was going smoothly with only minor glitches. Mostly due to the jboss specific XML that the IBM tutorial doesn't cover, and since I'm using XDoclet, that makes it even more complicated.
Now to the problem, or perhaps it's a question if this is the way things are supposed to work.
I have two business method CMP EJB's User and Group. I'll spare the full source unless it's requested, but the general gist of the CMP fields are:
UserBean:
public abstract String getEmail(); //primary key public abstract void setEmail(String email); public abstract String getPassword(); public abstract void setPassword(String password); public abstract GroupLocal getGroupMembership(); //CMR Field with group. public abstract void setGroupMembership(GroupLocal groupmembership);
GroupBean:
public abstract String getGroupName(); //primary key public abstract void setGroupName(String groupname); public abstract String getDescription(); public abstract void setDescription(String description); public abstract java.util.Collection getUsers(); //CMR field with user public abstract void setUsers(java.util.Collection users);
I then proceeded to define the relation with:
ejb-jar.xml
<ejb-relation > <ejb-relation-name>GroupsHaveUsers</ejb-relation-name> <ejb-relationship-role > <ejb-relationship-role-name>UsersInGroup</ejb-relationship-role-name> <multiplicity>Many</multiplicity> <relationship-role-source > <ejb-name>User</ejb-name> </relationship-role-source> <cmr-field > <cmr-field-name>groupMembership</cmr-field-name> </cmr-field> </ejb-relationship-role> <ejb-relationship-role > <ejb-relationship-role-name>GroupHasUsers</ejb-relationship-role-name> <multiplicity>One</multiplicity> <relationship-role-source > <ejb-name>Group</ejb-name> </relationship-role-source> <cmr-field > <cmr-field-name>users</cmr-field-name> <cmr-field-type>java.util.Collection</cmr-field-type> </cmr-field> </ejb-relationship-role> </ejb-relation>
jbosscmp-jdbc.xml
<ejb-relation> <ejb-relation-name>GroupsHaveUsers</ejb-relation-name> <foreign-key-mapping/> <ejb-relationship-role> <ejb-relationship-role-name>UsersInGroup</ejb-relationship-role-name> <key-fields/> </ejb-relationship-role> <ejb-relationship-role> <ejb-relationship-role-name>GroupHasUsers</ejb-relationship-role-name> <key-fields> <key-field> <field-name>groupname</field-name> <column-name>groupname</column-name> </key-field> </key-fields> </ejb-relationship-role> </ejb-relation>
Now the way I understand it the container should be handling the maintenence of this relationship right? So when I set the GroupMembership CMP field of a UserBean object, the container should add that UserObject to the corresponding Group object's Users Collection right? Or, since the relation should be bidirectional, if I add a UserLocal object to the Users Collection of a Group object then the corresponding User object's GroupMembership field should be updated with the latest GroupLocal, right?
This is not the case. After I set a User objects GroupMembership field with a GroupLocal object, the Group objects Users Collection is still null! If I manually instatiate the Group object, create a new Collection via a type cast from ArrayList and add the User object to the Group objects collection, then it works.
Is the relationship not working, or is this the way it should be done?