problem with flattened relationship

From: Steve Steinitz (steinit..atatactics.com.au)
Date: Thu Feb 05 2004 - 00:07:58 EST

  • Next message: Andrus Adamchik: "Re: problem with flattened relationship"

    Hello,

    We are trying to implement a flattened relationship. After adding
    an object to the relationship, a commit causes Cayenne to throw the
    following:

        org.objectstyle.cayenne.CayenneRuntimeException: [v.1.0.4
        December 14 2003] Raising from underlyingQueryEngine
        exception ...more stack trace below

    We've tried many permutations -- with and without To Dep PK, with
    and without foriegn key restraints...

    Previously I've used EOModeler with EOF to flatten relationships
    and so have never had to fully understand the many paramaters
    needed to get this to work.

    public void testUserRoleRelating()
    {
        System.out.println("Testworms.testUserRoleRelating: starting");
        DataContext aContext = app.cayenneContext();
        User user = (User) = aContext.createAndRegisterNewObject(className);
        aContext.commitChanges();
        Role role = (Role)Role.all().get(0);
        user.addToRoles(role);
        aContext.commitChanges(); // **** fails here ****
        assertTrue(user.getRoles().contains(role));
        aContext.deleteObject(user);
        aContext.commitChanges();
    }

    Here is the test case that causes the failure:

    My database schema looks like this:

    CREATE TABLE public.user (id integer NOT NULL, name text NULL, loginID text NULL, password text NULL, email text NULL, contactNumber text NULL, PRIMARY KEY (id));
    CREATE TABLE public.role (id integer NOT NULL, name text NULL, note text NULL, PRIMARY KEY (id));
    -- CREATE TABLE public.userroles (id integer NOT NULL, userid integer NOT NULL, roleid integer NOT NULL, PRIMARY KEY (id));

    CREATE TABLE public.userroles (userid integer NOT NULL, roleid integer NOT NULL, PRIMARY KEY (userid, roleid));

    ALTER TABLE public.userroles ADD FOREIGN KEY (userid) REFERENCES public.user (id);
    ALTER TABLE public.userroles ADD FOREIGN KEY (roleid) REFERENCES public.role (id);

    INSERT INTO AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('user', 200);
    INSERT INTO AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('role', 200);

    Our Cayenne model looks like:

    <?xml version="1.0" encoding="utf-8"?>
    <data-map project-version="1.0">
        <db-entity name="role" schema="public">
            <db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="4"/>
            <db-attribute name="name" type="LONGVARCHAR"/>
            <db-attribute name="note" type="LONGVARCHAR"/>
        </db-entity>
        <db-entity name="user" schema="public">
            <db-attribute name="contactnumber" type="LONGVARCHAR"/>
            <db-attribute name="email" type="LONGVARCHAR"/>
            <db-attribute name="id" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="4"/>
            <db-attribute name="loginid" type="LONGVARCHAR"/>
            <db-attribute name="name" type="LONGVARCHAR"/>
            <db-attribute name="password" type="LONGVARCHAR"/>
        </db-entity>
        <db-entity name="userroles" schema="public">
            <db-attribute name="roleid" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="4"/>
            <db-attribute name="userid" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="4"/>
        </db-entity>
        <obj-entity name="Role" className="com..model.Role" dbEntityName="role">
            <obj-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
            <obj-attribute name="note" type="java.lang.String" db-attribute-path="note"/>
        </obj-entity>
        <obj-entity name="User" className="com..model.User" dbEntityName="user">
            <obj-attribute name="contactNumber" type="java.lang.String" db-attribute-path="contactnumber"/>
            <obj-attribute name="email" type="java.lang.String" db-attribute-path="email"/>
            <obj-attribute name="loginid" type="java.lang.String" db-attribute-path="loginid"/>
            <obj-attribute name="name" type="java.lang.String" db-attribute-path="name"/>
            <obj-attribute name="password" type="java.lang.String" db-attribute-path="password"/>
        </obj-entity>
        <db-relationship name="userrolesArray" source="role" target="userroles" toDependentPK="false" toMany="true">
            <db-attribute-pair source="id" target="roleid"/>
        </db-relationship>
        <db-relationship name="userrolesArray" source="user" target="userroles" toDependentPK="false" toMany="true">
            <db-attribute-pair source="id" target="userid"/>
        </db-relationship>
        <db-relationship name="torole" source="userroles" target="role" toDependentPK="false" toMany="false">
            <db-attribute-pair source="roleid" target="id"/>
        </db-relationship>
        <db-relationship name="touser" source="userroles" target="user" toDependentPK="false" toMany="false">
            <db-attribute-pair source="userid" target="id"/>
        </db-relationship>
        <obj-relationship name="Roles" source="User" target="Role" toMany="true">
            <db-relationship-ref source="user" target="userroles" name="userrolesArray"/>
            <db-relationship-ref source="userroles" target="role" name="torole"/>
        </obj-relationship>
        <obj-relationship name="referencedByUsers" source="Role" target="User" toMany="true">
            <db-relationship-ref source="role" target="userroles" name="userrolesArray"/>
            <db-relationship-ref source="userroles" target="user" name="touser"/>
        </obj-relationship>
    </data-map>

    Thanks in advance for your time on this.

    Best regards,

    Steve Steinitz

    ---------------------------------------------------------------------------------------

    More stack strace:
        
    org.objectstyle.cayenne.CayenneRuntimeException: [v.1.0.4 December 14 2003] Raising from underlyingQueryEngine exception
    testUserRoleRelating(test.unit.TestWorms)org.objectstyle.cayenne.CayenneRuntimeException: [v.1.0.4 December 14 2003] Raising from underlyingQueryEngine exception.
    at org.objectstyle.cayenne.access.util.ContextCommitObserver.nextGlobalException(ContextCommitObserver.java:188)
    at org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:349)
    at org.objectstyle.cayenne.access.ContextCommit.commit(ContextCommit.java:188)
    at org.objectstyle.cayenne.access.DataContext.commitChanges(DataContext.java:764)
    at org.objectstyle.cayenne.access.DataContext.commitChanges(DataContext.java:738)
    at test.unit.TestWorms.testUserRoleRelating(TestWorms.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    Caused by: java.sql.SQLException: ERROR: $1 referential integrity violation - key in user still referenced from userroles

    at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:131)
    at org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connection.java:505)
    at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:320)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:48)
    at org.postgresql.jdbc1.AbstractJdbc1Statement.executeUpdate(AbstractJdbc1Statement.java:197)
    at org.objectstyle.cayenne.access.DataNode.runBatchUpdateAsIndividualQueries(DataNode.java:569)
    at org.objectstyle.cayenne.access.DataNode.runBatchUpdate(DataNode.java:477)
    at org.objectstyle.cayenne.access.DataNode.performQueries(DataNode.java:287)

    ---
    

    Steve Steinitz ph +61 (0)2 9487 7215 Director

    Data Tactics Sydney, Australia

    www.datatactics.com.au

    Web Commerce Development Project Estimation and Planning Software Development MacOS X Support



    This archive was generated by hypermail 2.0.0 : Thu Feb 05 2004 - 00:08:17 EST