Re: Simple question

From: Nanik Tolaram (nanikjav..otmail.com)
Date: Sun Mar 02 2003 - 11:33:54 EST

  • Next message: Andrus Adamchik: "Re: Simple question"

    Hi,

    Thanks for the reply. Actually the relationship is one-to-many (MODULES(1)
    and GROUP(many)). What I'm looking for is actually if we are inserting a new
    record into the MODULES table how can I insert a record into the GROUP table
    without creating new object for every new record that I want to insert. For
    example if I insert using the following code,

            ....
            ....
             group.setGroupname("ADMIN");

             module.setModulename("CUSTOMER");
             module.addToGroupsArray(group);

             ctxt.commitChanges(Level.DEBUG);

            ....
            ....

    With the code above the GROUP table will have a record of ADMIN and that
    particular is linked through the foreign key with te primary key of the
    MODULE table (containing the data CUSTOMER). Now, how do I insert additional
    record into the GROUP table without creating a new group object, something
    like the following

            ....
            ....
             group.setGroupname("ADMIN");

             module.setModulename("CUSTOMER");
             module.addToGroupsArray(group);

             ctxt.commitChanges(Level.DEBUG);

             group.setGroupname("USER")
             module.addToGroupsArray(group);
             ctxt.commitChanges(Level.DEBUG);

            ....
            ....

    Sorry if my question is bit long winded, just want to make sure I understand
    what I'm doing :)

    Thanks
    Nanik

    ----- Original Message -----
    From: "Andrus Adamchik" <andru..bjectstyle.org>
    To: "Nanik Tolaram" <nanikjav..otmail.com>
    Cc: <cayenne-use..bjectstyle.org>
    Sent: Sunday, March 02, 2003 11:13 PM
    Subject: Re: Simple question

    > Nanik,
    >
    > First according to your DataMap XML and code,, module has multiple
    > groups, so your description contradicts the map: Module has multiple
    > Groups, not the other way around, right?
    >
    > You are saying "I want to a similar record but different foreign key in
    > GROUP table", but you can't a foreign key without a corresponding
    > primary key in MODULES. So having an instance of Module object, either
    > new or existing one (and a corresponding MODULES record) is the only
    > way, if I correctly understand what you are trying to do here.
    >
    > This has nothing to do with Cayenne, this is simply how database
    > relationships work.
    >
    > Andrus
    >
    >
    >
    > Nanik Tolaram wrote:
    > > Hi,
    > >
    > > I have a 1-to-many relationship between GROUPS and MODULE (GROUP
    > > (1)----(N) MODULE). I want to a similar record but different foreign key
    > > in GROUP table, I have done like the following
    > >
    > >
    > > Groups group= (Groups)ctxt.createAndRegisterNewObject("Groups");
    > > Module module =
    (Module)ctxt.createAndRegisterNewObject("Module");
    > >
    > > group.setGROUPID(new Integer(1));
    > > group.setGroupname("ADMIN");
    > >
    > > module.setModulename("CUSTOMER");
    > > module.addToGroupsArray(group);
    > >
    > > ctxt.commitChanges(Level.DEBUG); // commit
    > >
    > >
    > > Module module1 =
    (Module)ctxt.createAndRegisterNewObject("Module");
    > > Groups group1=
    (Groups)ctxt.createAndRegisterNewObject("Groups");
    > >
    > > group1.setGROUPID(new Integer(1));
    > > group1.setGroupname("ADMIN");
    > >
    > > module1.setModulename("PRODUCT");
    > > module1.addToGroupsArray(group1);
    > >
    > > ctxt.commitChanges(Level.DEBUG); // commit
    > >
    > > and the following is the datamap.map.xml config file,
    > >
    > > <?xml version="1.0" encoding="UTF-8"?>
    > > <data-map project-version="1.0">
    > >
    > > <db-entity name="USERS">
    > > <db-attribute name="login_name" type="CHAR"
    > > isMandatory="true" length="20"/>
    > > <db-attribute name="userid" type="INTEGER"
    > > isPrimaryKey="true" isMandatory="true"/>
    > > </db-entity>
    > >
    > > <db-entity name="GROUPS">
    > > <db-attribute name="GROUPID" type="INTEGER"
    > > length="4"/>
    > > <db-attribute name="groupname" type="CHAR"
    > > length="50"/>
    > > <db-attribute name="MODULEID" type="INTEGER"
    > > length="4"/>
    > > </db-entity>
    > >
    > > <db-entity name="MODULE">
    > > <db-attribute name="MODULEID" type="INTEGER"
    > > isPrimaryKey="true" isMandatory="true"/>
    > > <db-attribute name="modulename" type="CHAR"
    > > length="50"/>
    > > </db-entity>
    > >
    > >
    > >
    > > <obj-entity name="Groups" className="Groups"
    > > dbEntityName="GROUPS">
    > > <obj-attribute name="GROUPID"
    > > type="java.lang.Integer" db-attribute-name="GROUPID"/>
    > > <obj-attribute name="groupname"
    > > type="java.lang.String" db-attribute-name="groupname"/>
    > > </obj-entity>
    > >
    > > <obj-entity name="Users" className="Users"
    > > dbEntityName="USERS">
    > > <obj-attribute name="login_name"
    > > type="java.lang.String" db-attribute-name="login_name"/>
    > > </obj-entity>
    > >
    > > <obj-entity name="Module" className="Module"
    > > dbEntityName="MODULE">
    > > <obj-attribute name="modulename"
    > > type="java.lang.String" db-attribute-name="modulename"/>
    > > </obj-entity>
    > >
    > >
    > >
    > >
    > > <db-relationship name="groupsArray" source="MODULE"
    > > target="GROUPS" toDependentPK="false" toMany="true">
    > > <db-attribute-pair source="MODULEID"
    > > target="MODULEID"/>
    > > </db-relationship>
    > >
    > > <db-relationship name="toGroups" source="GROUPS"
    > > target="MODULE" toDependentPK="false" toMany="false">
    > > <db-attribute-pair source="MODULEID"
    > > target="MODULEID"/>
    > > </db-relationship>
    > >
    > >
    > >
    > >
    > > <obj-relationship name="groupsArray" source="Module"
    > > target="Groups" toMany="true" deleteRule="Nullify">
    > > <db-relationship-ref source="MODULE"
    > > target="GROUPS" name="groupsArray"/>
    > > </obj-relationship>
    > >
    > > <obj-relationship name="toGroups" source="Groups"
    > > target="Module" toMany="false" deleteRule="Nullify">
    > > <db-relationship-ref source="GROUPS"
    > > target="MODULE" name="toGroups"/>
    > > </obj-relationship>
    > >
    > > </data-map>
    > >
    > > Are there any other way to make things easier if I want to insert same
    > > record with different foreign key ?
    > >
    > > Thanks
    > > Nanik
    > >
    > >
    > >
    > >
    > >
    > >
    >



    This archive was generated by hypermail 2.0.0 : Sun Mar 02 2003 - 11:34:53 EST