3 Replies Latest reply on Nov 21, 2003 1:09 PM by txiki

    Working Examples of Bidirectional CMR on JBoss 3.2.x Generat

    txiki

      After much tail chaising with JBoss 3.2.x, Many-to-Many CMR, & XDoclet v1.2b3 (much searching, reading and trial-and-error), i've decided that i'm in serious need of guidance...

      I'm trying to implement what i believe is a bidirectional many-to-many cmr cmp relationship between two entities:

      1) UserBean and
      2) RoleBean

      Their relationship can be described as: "any/many users can have any/many roles".

      I've implemented the 2 entities listed above, in addition to a UserRole entity which functions as a xwalk table between User and Role. Thus far, i've been happily using them as a JBoss JAAS authentication dataSource for the last month, w/o declaring any kind of cmr relationship between them. What i want to do now is declare relationships between entities so that:

      1) when i find a user, i can get a collection of their roles as an attribute of the user object (instead of a separate lookup in the role entity),
      2) when i find a role, i can get a collection of the users who have this role, expressed as an attribute of the roles entity
      3) when i delete a user, i want their delete to cascade and remove all of their roles, but not vice versa (removing a role does not delete the user(s) associated with it)

      Does anybody have a working example of a bidirectional Many-to-Many relationship that works with JBoss 3.2.1 that they would care to share with me?

      Even if you don't have a working example, feel free to let me know if i'm not describing my problem very well or am not using the correct terminology.

      Thanks,

      Rick

        • 1. Re: Working Examples of Bidirectional CMR on JBoss 3.2.x Gen

          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

            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

              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!