Modeler question for one-to-one logical mapping

From: Michael Amster (mamste..ebeasy.com)
Date: Sat Jul 05 2003 - 22:53:35 EDT

  • Next message: Andrus Adamchik: "Re: Modeler question for one-to-one logical mapping"

    Hi:

    I have a lookup table which is used as a logical one to one (not in the
    cayenne sense of FK). I am having a hard time understanding how to use
    the cayenne modeler to get the relationship correct so that I get back
    only the lookup value I need. The database tables are as follows:

    CREATE TABLE RaceDBEntity (location VARCHAR(255) NOT NULL, race_date
    DATE NOT NULL, race_id INT NOT NULL, race_name VARCHAR(255) NOT NULL,
    race_type_id INT NOT
    NULL, year VARCHAR(4) NOT NULL, PRIMARY KEY (race_id))
                                                                                    
    CREATE TABLE RaceTypeDbEntity (race_type_id INT NOT NULL, type_name
    VARCHAR(255) NOT NULL, PRIMARY KEY (race_type_id))
                                                                                    
    RaceTypes are just lookups and each race has a single race type. If I
    create a RaceTypeDbEntity relation in RaceDbEntity that is to Dep PK,
    and in RaceDbEntity to RaceTypeDbEntity that is To Many, I get the
    following error when I add a race with the below code:

    Caused by: org.objectstyle.cayenne.CayenneException: [v.1.0b4 June 16
    2003] Error generating PK: More than one row for 'RaceDBEntity'
            at org.objectstyle.cayenne.access.util.PrimaryKeyHelper.createPermIdsForObjEntity(PrimaryKeyHelper.java:200)
            at org.objectstyle.cayenne.access.ContextCommit.createPrimaryKeys(ContextCommit.java:517)
            at org.objectstyle.cayenne.access.ContextCommit.commit(ContextCommit.java:139)
            at org.objectstyle.cayenne.access.DataContext.commitChanges(DataContext.java:732)
            ... 42 more
    Caused by: java.sql.SQLException: More than one row for 'RaceDBEntity'
            at org.objectstyle.cayenne.dba.mysql.MySQLPkGenerator.getPrimaryKey(MySQLPkGenerator.java:166)
            at org.objectstyle.cayenne.dba.mysql.MySQLPkGenerator.pkFromDatabase(MySQLPkGenerator.java:93)
            at org.objectstyle.cayenne.dba.JdbcPkGenerator.generatePkForDbEntity(JdbcPkGenerator.java:322)
            at org.objectstyle.cayenne.access.util.PrimaryKeyHelper.createPermIdsForObjEntity(PrimaryKeyHelper.java:195)

    code snippet:
           BeanUtils.copyProperties(race, raceForm);
           RaceType raceType =
            LookupDao.loadRaceTypeById(ctxt,raceForm.getRaceType());
           race.setToRaceType(raceType);
           ctxt.commitChanges(org.apache.log4j.Level.WARN);

    The xml file looks like this:

        <db-entity name="RaceDBEntity">
            <db-attribute name="location" type="VARCHAR" isMandatory="true"
    length="255"/>
            <db-attribute name="race_date" type="DATE" isMandatory="true"/>
            <db-attribute name="race_id" type="INTEGER" isPrimaryKey="true"
    isMandatory="true"/>
            <db-attribute name="race_name" type="VARCHAR" isMandatory="true"
    length="255"/>
            <db-attribute name="race_type_id" type="INTEGER"
    isMandatory="true"/>
            <db-attribute name="year" type="VARCHAR" isMandatory="true"
    length="4"/> </db-entity>
        <db-entity name="RaceTypeDbEntity">
            <db-attribute name="race_type_id" type="INTEGER"
    isPrimaryKey="true" isMandatory="true"/>
            <db-attribute name="type_name" type="VARCHAR" isMandatory="true"
    length="255"/>
        </db-entity>

    .....

        <db-relationship name="toRace" source="RaceTypeDbEntity"
    target="RaceDBEntity" toDependentPK="false" toMany="true">
            <db-attribute-pair source="race_type_id" target="race_type_id"/>
        </db-relationship>
        <db-relationship name="toRaceType" source="RaceDBEntity"
    target="RaceTypeDbEntity" toDependentPK="true" toMany="false">
            <db-attribute-pair source="race_type_id" target="race_type_id"/>
        </db-relationship>

    Any clue as to how to get this to work is greatly appreciated. I have
    the many-to-many situations working fine based on the example in the
    tutorial.

    -MA



    This archive was generated by hypermail 2.0.0 : Sat Jul 05 2003 - 22:53:23 EDT