voidinit
12-10-2003, 01:48 PM
The JBoss forums suck, too many questions and not nearly enough people with answers. I don't know if anyone who reads these forums have ever studied J2EE development, but I'm willing to bet there is at least one.
I have two CMP beans UserEJB and RoleEJB that persist fields "email", "password", "roles" and "roleName", "description" respectivley. I have set up a container managed many to many bidirectional relationship between the two. Since this relationship is many to many, I have setup up relationship table mapping using "email" the primary key field of UserEJB and roleName the primary key field of RoleEJB. Basically the roles field of UserEJB should return a java.util.Collection containing any roles a user is in.
Here is my first question. If I create a new user like.
UserLocalHome userHome = getUserLocalHome();
UserLocal user = userHome.create("Me", "MyPassword");
This will create a new database entry in the 'user' table with the appropriate values for email and password set, but the 'roles' field is null!
Since the roles field is null, when I try to add a role to a user as in the below example, roles.add() will throw a null pointer exception.
UserLocalHome userHome = getUserLocalHome();
RoleLocalHome roleHome = getRoleHome();
RoleLocal newRole = roleHome.create("Everybody", "Default Role");
UserLocal user = userHome.findByPrimaryKey("me");
Collection roles = user.getRoles();
roles.add(newRole);
roles.setRoles(role);
Here is my question.
1. If I defined the roles getter and setter and type as java.util.Collection is the container supposed to return an empty collection if the value is not set?
2. How is the relationship mapping table used? So far I have seen no data in it which tells me my relationship may be described wrong in the deployment .xml files. (ejb-jar.xml and jbosscmp-jdbc.xml)
3. If question #1 is false and I'm responsible for setting an initial empty Collection in the roles field, how the heck do I get an inital empty collection. I have tried the following:
UserLocalHome userHome = getUserLocalHome();
RoleLocalHome roleHome = getRoleLocalHome();
Collection c = (Collection) new Vector();
/* vector is one of the few classes
* that cast to collection since
* collection cannot be instatiated.
*/
UserLocal user = userHome.create("you", "password");
//The roles field in the DB is null
user.setRoles(c);
// The roles field in the database is now not null.
Collection roles = user.getRoles();
//A collection with 0 elements is returned.
RoleLocal role = roleHome.findByPrimaryKey("Everybody");
roles.add(role);
//Collection Role now has one element.
user.setRoles(role);
//roles in DB is still non-null
Collection inRoles = user.getRoles();
//A Collection with 0 elements is returned!!!!! What happend to the elements?
As for specifics, I'm using Jboss 3.2, I've Jboss configured to use MySQL with default mysql mappings. I succesfully have one-to-one cmr working.
I've attached my source tree in case anyone wants to take a look at it.
I have two CMP beans UserEJB and RoleEJB that persist fields "email", "password", "roles" and "roleName", "description" respectivley. I have set up a container managed many to many bidirectional relationship between the two. Since this relationship is many to many, I have setup up relationship table mapping using "email" the primary key field of UserEJB and roleName the primary key field of RoleEJB. Basically the roles field of UserEJB should return a java.util.Collection containing any roles a user is in.
Here is my first question. If I create a new user like.
UserLocalHome userHome = getUserLocalHome();
UserLocal user = userHome.create("Me", "MyPassword");
This will create a new database entry in the 'user' table with the appropriate values for email and password set, but the 'roles' field is null!
Since the roles field is null, when I try to add a role to a user as in the below example, roles.add() will throw a null pointer exception.
UserLocalHome userHome = getUserLocalHome();
RoleLocalHome roleHome = getRoleHome();
RoleLocal newRole = roleHome.create("Everybody", "Default Role");
UserLocal user = userHome.findByPrimaryKey("me");
Collection roles = user.getRoles();
roles.add(newRole);
roles.setRoles(role);
Here is my question.
1. If I defined the roles getter and setter and type as java.util.Collection is the container supposed to return an empty collection if the value is not set?
2. How is the relationship mapping table used? So far I have seen no data in it which tells me my relationship may be described wrong in the deployment .xml files. (ejb-jar.xml and jbosscmp-jdbc.xml)
3. If question #1 is false and I'm responsible for setting an initial empty Collection in the roles field, how the heck do I get an inital empty collection. I have tried the following:
UserLocalHome userHome = getUserLocalHome();
RoleLocalHome roleHome = getRoleLocalHome();
Collection c = (Collection) new Vector();
/* vector is one of the few classes
* that cast to collection since
* collection cannot be instatiated.
*/
UserLocal user = userHome.create("you", "password");
//The roles field in the DB is null
user.setRoles(c);
// The roles field in the database is now not null.
Collection roles = user.getRoles();
//A collection with 0 elements is returned.
RoleLocal role = roleHome.findByPrimaryKey("Everybody");
roles.add(role);
//Collection Role now has one element.
user.setRoles(role);
//roles in DB is still non-null
Collection inRoles = user.getRoles();
//A Collection with 0 elements is returned!!!!! What happend to the elements?
As for specifics, I'm using Jboss 3.2, I've Jboss configured to use MySQL with default mysql mappings. I succesfully have one-to-one cmr working.
I've attached my source tree in case anyone wants to take a look at it.