Re: Many to Many Relation

From: Mike Kienenberger (mkienen..laska.net)
Date: Wed Jun 29 2005 - 12:43:31 EDT

  • Next message: Michael Gentry (Yes, I'm a Contractor): "Re: Relation null??"

    Dhruti Ramani <dhrutiraman..ahoo.com> wrote:
    > In my object model I have two objects UserRole and SystemUser. I need to
    have Many to Many relation between them. UserRole can have any no. of
    systemUsers and SystemUser can have many roles. I have added a foreign
    key(userRole1_id) in UserRole and systemUser_id is the primary key for
    SystemUser. Here is what I have tried.....
    >
    > Relation From To Join
    > users UserRole SystemUser (userRole1_id ---
    systemUser_id) ("to_dep_key checked)
    > roles SystemUser UserRole
    (systemUser_id----userRole1_id)
    >
    > But when I create those two objects and try to add user to userRole or
    userRole to user, it does not persist in the database. Individually both
    objects are there in the database but there is no relation between them and
    I don't get any errors.

    Yes, this is well-supported in Cayenne and is called a flattened
    relationship. Check the user guide for full details, but in generaly,
    here's what you'll need to do:

    Make sure you have DbEntities for USER_ROLE, SYSTEM_USER, and
    SYSTEM_USER_ROLE (or whatever your join table is called).

    Set up relationships between USER_ROLE and SYSTEM_USER_ROLE and
    relationships between SYSTEM_USER and SYSTEM_USER_ROLE.

    Note that the "to_dep_key" must be set on the USER_ROLE and in SYSTEM_USER
    DbRelationships rather than in SYSTEM_USER_ROLE relationships.

    After you've done this, you will only need ObjEntities for UserRole and
    SystemUser.

    You should be able to set up a to-many relationship directly between
    USER_ROLE and SYSTEM_USER, and it should automatically pick up the two-step
    join from the DbRelationships. (ie, USER_ROLE.USER_ROLE_ID ->
    SYSTEM_USER_ROLE.USER_ROLE_ID and SYSTEM_USER_ROLE.SYSTEM_USER_ID ->
    SYSTEM_USER.SYSTEM_USER_ID.)

    -Mike



    This archive was generated by hypermail 2.0.0 : Wed Jun 29 2005 - 12:40:44 EDT