Re: Many to Many Relation

From: Todd O'Bryan (toddobrya..ac.com)
Date: Thu Jun 30 2005 - 11:18:38 EDT

  • Next message: Dhruti Ramani: "Re: Many to Many Relation"

    I had a problem with this myself; I think we could improve the docs
    on this point. Let me see if I can clarify a bit.

    I'm going to call your two tables role and user and their associated
    classes Role and User. Assume both tables have an id column that
    Cayenne handles for you automatically.

    1. Create a new dbEntity. I usually call mine "userToRole", but you
    can choose anything that indicates it's a join table.
    2. Include two attributes: "roleId" and "userId", both of type
    Integer. That's all.
    3. Create two relationships. I would call them "toRole" and "toUser".
    Target should be "role" and "user", respectively, and source and
    target in the relationships should be "roleId", "id" and "userId",
    "id", respectively. I call the reverse relationships "toUserToRole".

    You now have relationships from each dbEntity to the join table, and
    from the join table to each dbEntity. All you have to do is teach
    Cayenne to follow those relationships and you're set.

    4. Select the Role objEntity and sync it with the dbEntity. You have
    to add an extra relationship to Role that combines the two partial
    relationships. I'd call it "users". Choose "User" as the target.
    5. When you click the Info button to specify the relationship, you'll
    need to select a DbRelatioship. Choose "toUserToRole" which is a
    mapping from "role -> userToRole". When you do that, Cayenne should
    fill in the "toUser" relation from "userToRole -> user". Click done.
    6. Do the same for the User objEntity.
    7. Generate the classes again.

    Voila. You have a flattened relationship and you should have the
    methods you're looking for.

    Todd

    P.S. Let me know if anything goes wrong above and I'll try to fix it.

    On Jun 30, 2005, at 10:08 AM, Dhruti Ramani wrote:

    >
    > I am so confused now. if you have same set up then how
    > come it does not generate add/remove methods for me
    > for M to M relation?
    >
    > I am attaching *map.xml and cayenne.xml, if some one
    > can look at that figure out what I am doing wrong,
    > that would be really gr8 help. Objects I am interested
    > are SystemUser and UserRole and the join table is
    > UserRole_SystemUser.
    >
    > Thanks,
    > Denna
    > --- Mike Kienenberger <mkienen..laska.net> wrote:
    >
    >
    >> Dhruti Ramani <dhrutiraman..ahoo.com> wrote:
    >>
    >>> I thought so but I read the userguide and
    >>>
    >> misunderstood it. Now i can have
    >> M to M relations between UserRole and SystemUser.
    >> Thank you so much.
    >>
    >>>
    >>> Another Problem . When I generate those two
    >>>
    >> classes I don't get any add or
    >> remove methods, I just got getUsers() in UserRole
    >> and getRoles() in
    >> SystemUser.
    >>
    >>>
    >>> In userguide I read somewhere that "Most of
    >>>
    >> flattened relationships are
    >> treated as read-only by Cayenne. Only one (the most
    >> commonly used) type is
    >> read/write. It is many-to-many (n:m) flattened
    >> relationships with a single
    >> join table."
    >>
    >>>
    >>> So how do I insert values to those objects?
    >>>
    >>
    >> No, a simple many-to-many relationship like that
    >> should automatically
    >> generate the add/remove methods.
    >>
    >> I have an almost identical setup and it generates
    >> code like this:
    >>
    >> public void addToRoleList(SecRole obj) {
    >> addToManyTarget("roleList", obj, true);
    >> }
    >> public void removeFromRoleList(SecRole obj) {
    >> removeToManyTarget("roleList", obj, true);
    >> }
    >> public List getRoleList() {
    >> return (List)readProperty("roleList");
    >> }
    >>
    >> [From the code of the equivalent class as your
    >> SystemUser with SecRole being
    >> the same as UserRole]
    >>
    >> -Mike
    >>
    >>
    >
    >
    >
    >
    > ____________________________________________________
    > Yahoo! Sports
    > Rekindle the Rivalries. Sign up for Fantasy Football
    > http://football.fantasysports.yahoo.com
    >



    This archive was generated by hypermail 2.0.0 : Thu Jun 30 2005 - 11:18:42 EDT