cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Kienenberger <mkien...@gmail.com>
Subject Re: Cayenne Tutorial: INSERT on table 'PAINTING' caused a violation of foreign key constraint
Date Tue, 26 Oct 2010 17:23:37 GMT
In fact, this could be the problem.

By calling it twice, you will get two of each object in each relationship.
This might be causing your foreign key constraint error.


On Tue, Oct 26, 2010 at 1:02 PM, Borut Bolčina <borut.bolcina@gmail.com> wrote:
> Hi,
>
> how did you create your database (show us the create statements)? Which
> database are you using? Foreign key constraints are optional, but you need
> them if you want to reverse engineer the database, so that relationships in
> the modeler are created.
>
> Also, in your unit test, you are setting
>
>           picasso.addToPaintings(girl);
>           picasso.addToPaintings(stein);
>
> but this is not needed. Cayenne automatically sets the other side of the
> relationship for you, unlike Hibernate.
>
> -Borut
>
> 2010/10/26 caden whitaker <caden.whitaker@gmail.com>
>
>> 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);
>>    }
>>    @SuppressWarnings("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");
>>    }
>>
>

Mime
View raw message