Re: many to many relation

From: Marcin Skladaniec (marci..sh.com.au)
Date: Fri Dec 08 2006 - 17:59:18 EST

  • Next message: marco turchi: "Re: many to many relation"

    Hi
    just check if in your model (xxx.map.xml) you have something like that :

            <db-entity name="Entity">
                    <db-attribute name="id" type="INTEGER" isPrimaryKey="true"
    isMandatory="true"/>
                    more stuff
            </db-entity>
            <db-entity name="NameRelation">
                    <db-attribute name="entityId" type="INTEGER" isPrimaryKey="true"
    isMandatory="true"/>
                    <db-attribute name="nameId" type="INTEGER" isPrimaryKey="true"
    isMandatory="true"/>
                    eventually more stuff
            </db-entity>
                    <db-entity name="Name"> <db-attribute name="id" type="INTEGER"
    isPrimaryKey="true" isMandatory="true"/>
                    more stuff
            </db-entity>
            

    a bit further down :

            <obj-entity name="Entity" className="Entity" dbEntityName="Entity">
                    stuff here
            </obj-entity>
            <obj-entity name="NameRelation" className="NameRelation" >
                    stuff here
            </obj-entity>
            <obj-entity name="Name" className="Name" dbEntityName="Name">
                    stuff here
            </obj-entity>

    and now the most important:

            <db-relationship name="entities" source="Name" target="NameRelation"
    toDependentPK="true" toMany="true">
                    <db-attribute-pair source="id" target="nameId"/>
            </db-relationship>
            <db-relationship name="names" source="Entity" target="NameRelation"
    toDependentPK="true" toMany="true">
                    <db-attribute-pair source="id" target="entityId"/>
            </db-relationship>
            <db-relationship name="entity" source="NameRelation" target="Entity"
    toMany="false">
                    <db-attribute-pair source="entityId" target="id"/>
            </db-relationship>
            <db-relationship name="name" source="NameRelation" target="Name"
    toMany="false">
                    <db-attribute-pair source="nameId" target="id"/>
            </db-relationship>

    <obj-relationship name="entities" source="Name" target="Entity"
    deleteRule="Deny" db-relationship-path="entities.entity"/>
    <obj-relationship name="names" source="Entity" target="Name"
    deleteRule="Deny" db-relationship-path="names.name"/>

            
    and now in Java:

    Entity e = getContext().newObject(Entity.class);
    Name n getContext().newObject(Name.class);
    e.addToNames(n);
    getContext().commitChanges();

    This should do.
    Marcin

    On 09/12/2006, at 1:53 AM, marco turchi wrote:

    > Hi,
    > I'm a new Cayenne user and I'm trying to create a many to many
    > relationship.
    > This is the whole situation:
    > Entity<<->>NameRelation<<->>Name
    >
    > NameRelation has a composed key id_entity, id_name
    >
    > I create the model where
    > Entity has a "to many" relationship to Name, and a relationship that
    > forwards its key to NameRelation (the flag "To Dep PK" enabled)
    >
    > Name has a relationship that forwards its key to NameRelation (the
    > flag "To
    > Dep PK" enabled)
    >
    > NameRelation has two "to many" relationship directed to Name and
    > Entity.
    >
    > The problem is that when I insert an element into Entity and into
    > Name, the
    > software did not insert automatically the row into NameRelation.
    >
    > maybe it is an easy question for you, but not for me... :-(
    > please, can you help me?
    >
    > Thanks a lot
    > Marco

    -------------------------->
    ish
    http://www.ish.com.au
    Level 1, 30 Wilson Street Newtown 2042 Australia
    phone +61 2 9550 5001 fax +61 2 9550 4001



    This archive was generated by hypermail 2.0.0 : Fri Dec 08 2006 - 18:01:03 EST