Many-to-Many CMR Questions
john_anderson_ii Dec 9, 2003 11:13 AMI am trying to set up a many-to-many CMR between two CMP EJBs.
I have RoleBean which persists two fields "name" and "description". I also have UserBean which perisists three fields, "email", "password", and "role".
I have setup relation table mapping and that table has two fields "email", the pk from UserBean and "name" the pk from RoleBean.
Question 1:
The method java.util.Collection UserBean.getRole() returns null!
eg.
Collection c = userBean.getRole(); //c == null!
I can look in the MySQLcc application and see the field value is null. The reason, I believe that c == null in the above example is because I have not set any roles. When a user is created the only info supplied is email and password. When I try to add a user to a role I do it like this.
user = userHome.findByPrimaryKey(email);
role = roleHome.findByPrimaryKey(name);
Collection c = user.getRole();
c.add(role);
However, this will throw a NullPointerException because c is null.
Should the container be responsibile for initializing the "role" field to return a java.util.Collection when getRole() is called?
If this is the case then my deployment descriptors for the relationship are messed up.
Question 2:
If the container is not responsible for initialy setting an empty java.util.Collection in the "roles" field, how do I go about creating an empty Collection?
If I try something like.
user = userHome.create(email, password);
role = roleHome.findByPrimaryKey(name);
c = Collections.synchronizedCollection((Collection) new Vector());
/*Vector is one of the few classes that I know to cast to a Collection. But watch what happens to it. */
c.add(new Object(this)) // c has one element;
c.add(new Object(that)) //c has two elements;
user.setRole(c); //c has been set.
Collection c = user.getRole();
/* c has no elements! An empty collection has been returned */
I'm really confused at this! Plus for the entire two days I've been trying to get this right, never once have I seen any data go into the table I set up for the relationship-table-mapping.
Any help in this are would be very appreciated.