Cayenne Tutorial: INSERT on table 'PAINTING' caused a violation of foreign key constraint

From: caden whitaker (caden.whitake..mail.com)
Date: Tue Oct 26 2010 - 16:24:25 UTC

  • Next message: Borut Bolčina: "Re: Cayenne Tutorial: INSERT on table 'PAINTING' caused a violation of foreign key constraint"

    Hey all,

    Running through the tutorials, I know what that error means, but I don't
    think I've done anything wrong. Can someone take a quick look at this
    xml/object set and tell me what I did wrong? Any help would be greatly
    appreciated.

    Mapping.xml
    <?xml version="1.0" encoding="utf-8"?>
    <data-map xmlns="http://cayenne.apache.org/schema/3.0/modelMap"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://cayenne.apache.org/schema/3.0/modelMap
    http://cayenne.apache.org/schema/3.0/modelMap.xsd"
      project-version="3.0.0.1">
        <property name="defaultPackage" value="org.example.cayenne.persistent"/>
        <db-entity name="ARTIST">
            <db-attribute name="ID" type="BIGINT" isPrimaryKey="true"
    isMandatory="true"/>
            <db-attribute name="Name" type="VARCHAR" length="255"/>
        </db-entity>
        <db-entity name="PAINTING">
            <db-attribute name="ID" type="BIGINT" isPrimaryKey="true"
    isMandatory="true"/>
            <db-attribute name="Name" type="VARCHAR" length="255"/>
        </db-entity>
        <obj-entity name="Artist"
    className="main.java.org.example.cayenne.persistent.Artist"
    dbEntityName="ARTIST">
            <obj-attribute name="name" type="java.lang.String"
    db-attribute-path="Name"/>
        </obj-entity>
        <obj-entity name="Painting"
    className="main.java.org.example.cayenne.persistent.Painting"
    dbEntityName="PAINTING">
            <obj-attribute name="name" type="java.lang.String"
    db-attribute-path="Name"/>
        </obj-entity>
        <db-relationship name="paintings" source="ARTIST" target="PAINTING"
    toMany="true">
            <db-attribute-pair source="ID" target="ID"/>
        </db-relationship>
        <db-relationship name="artist" source="PAINTING" target="ARTIST"
    toMany="false">
            <db-attribute-pair source="ID" target="ID"/>
        </db-relationship>
        <obj-relationship name="paintings" source="Artist" target="Painting"
    deleteRule="Deny" db-relationship-path="paintings"/>
        <obj-relationship name="artist" source="Painting" target="Artist"
    deleteRule="Deny" db-relationship-path="artist"/>
    </data-map>

    _Artist.java
    public abstract class _Artist extends CayenneDataObject {

        public static final String NAME_PROPERTY = "name";
        public static final String PAINTINGS_PROPERTY = "paintings";

        public static final String ID_PK_COLUMN = "ID";

        public void setName(String name) {
            writeProperty("name", name);
        }
        public String getName() {
            return (String)readProperty("name");
        }

        public void addToPaintings(Painting obj) {
            addToManyTarget("paintings", obj, true);
        }
        public void removeFromPaintings(Painting obj) {
            removeToManyTarget("paintings", obj, true);
        }
       ..uppressWarnings("unchecked")
        public List<Painting> getPaintings() {
            return (List<Painting>)readProperty("paintings");
        }

    }

    _Painting.java
    public abstract class _Painting extends CayenneDataObject {

        public static final String NAME_PROPERTY = "name";
        public static final String ARTIST_PROPERTY = "artist";

        public static final String ID_PK_COLUMN = "ID";

        public void setName(String name) {
            writeProperty("name", name);
        }
        public String getName() {
            return (String)readProperty("name");
        }

        public void setArtist(Artist artist) {
            setToOneTarget("artist", artist, true);
        }

        public Artist getArtist() {
            return (Artist)readProperty("artist");
        }

    }

    JUnit test case:
        // JUnit
        public void testBuild()
            throws Exception
        {
            System.out.println("Begin Test");
            try {
                ObjectContext context = DataContext.createDataContext();
                Artist picasso = context.newObject(Artist.class);
                picasso.setName("Pablo Picasso");

                Painting girl = context.newObject(Painting.class);
                girl.setName("Girl Reading at a Table");
                girl.setArtist(picasso);

                Painting stein = context.newObject(Painting.class);
                stein.setName("Gertrude Stein");
                stein.setArtist(picasso);

                picasso.addToPaintings(girl);
                picasso.addToPaintings(stein);

                context.commitChanges();

            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("End Test");
        }



    This archive was generated by hypermail 2.0.0 : Tue Oct 26 2010 - 16:24:55 UTC