-
1. Re: Working Examples of Bidirectional CMR on JBoss 3.2.x Gen
lido Nov 21, 2003 10:56 AM (in response to txiki)I don't think cascade-delete does what you think it does. You can only do a cascade-delete when you have a one-one relationship, or a one-many relationship when you're deleting the entities on the many side of it. Many-many cascade deletes aren't possible, and generally don't make sense.
For instance, if your Users had multiple Addresses (one-many), you could could cascade-delete the Addresses. What you are removing is the actual Address objects, not just the reference to them. This make sense in the case of Addresses, because you would want their lifetime linked with the lifetime of the Users.
You probably wouldn't want a Role entity to actually be deleted when ever you deleted a User, because all the other users of that type would lose that Role. -
2. Re: Working Examples of Bidirectional CMR on JBoss 3.2.x Gen
lido Nov 21, 2003 11:12 AM (in response to txiki)At any rate, here's an example of a many-many bidirectional relationship using XDoclet. I've taken a real example and changed the names to more closely match your problem.
On the UserBean side:
/**
* Gets the Roles of a User
*
* @return The Roles
*
* @ejb.interface-method view-type="local"
* @ejb.relation
* name="User-Role"
* role-name="user-has-role"
* @jboss.relation-table
* table-name="USER_TO_ROLE_TABLE"
* create-table="no"
* remove-table="no"
* @jboss.relation
* related-pk-field="roleId"
* fk-column="ROLE_ID"
*/
public abstract java.util.Collection getRoles();
On the RoleBean side:
/**
* Gets the Users acting in this Role
*
* @return The Users
*
* @ejb.interface-method view-type="local"
* @ejb.relation
* name="User-Role"
* role-name="role-has-user"
* @jboss.relation-table
* table-name="USER_TO_ROLE_TABLE"
* create-table="no"
* remove-table="no"
* @jboss.relation
* related-pk-field="userId"
* fk-column="USER_ID"
*/
public abstract java.util.Collection getUsers(); -
3. Re: Working Examples of Bidirectional CMR on JBoss 3.2.x Gen
txiki Nov 21, 2003 1:09 PM (in response to txiki)Lido,
You're right, I wasn't real clear. What I wanted was to have a user delete to cascade to the user_x_role table, essentially removing the crosswalk records, but not to have it delete the roles records.
Since I had such a hard time getting my head around creating a M:M using xdoclet, I ended up implementing it as two 1:M relationships. While it does work for me that way, I'm currently managing (in code) the relationships between the two 1:M relationships ( user => user_x_role, and role => user_x_role), and it strikes me as a bit ungainly/ugly if not downright kludgey.
Thanks for the code sample, I'll give it a try when I revisit my usermgr code after I clear out the new work that just crossed my desk.
Cheers!