[ 
https://issues.apache.org/jira/browse/CAY-1751?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13484392#comment-13484392
 ] 

Andrus Adamchik commented on CAY-1751:
--------------------------------------

So I created a 3.1 project based on your 3.0 mapping (the parts we are talking 
about should be compatible), and switched from Oracle to Derby to simplify my 
environment. The error is reproducible in this setup...  Next I did a small fix 
to the test, registering object before setting its relationship. Doesn't make 
much difference here I think, but just a good practice in general:

@@ -103,12 +104,10 @@ public class Main {
             DataObject treeType = treeTypes.get(0);
             DataObject enclosure = enclosures.get(0);
 
-            DataObject fruitTree = new CayenneDataObject();
-            fruitTree.setObjectId(new ObjectId("FruitTree"));
+            DataObject fruitTree = (DataObject) context.newObject("FruitTree");
             fruitTree.writeProperty("plantingDate", new Date());
             fruitTree.writeProperty("toEnclosure", enclosure);
             fruitTree.writeProperty("toTreeType", treeType);
-            context.registerNewObject(fruitTree);


At this point the error is still reproducible. Finally I changed the mapping to 
add "fake" PKs to 2 entities and specify which side is the master, and which is 
dependent:

diff --git a/src/main/resources/FieldDomainMap.map.xml 
b/src/main/resources/FieldDomainMap.map.xml
index e76fd96..530f8b2 100644
--- a/src/main/resources/FieldDomainMap.map.xml
+++ b/src/main/resources/FieldDomainMap.map.xml
@@ -14,7 +14,7 @@
                </db-key-generator>
        </db-entity>
        <db-entity name="FRUIT_TREES">
-               <db-attribute name="CODE_TYPE_TREE" type="NUMERIC" 
isMandatory="true" length="3"/>
+               <db-attribute name="CODE_TYPE_TREE" type="NUMERIC" 
isPrimaryKey="true" isMandatory="true" length="3"/>
                <db-attribute name="ID_ENCLOSURE" type="NUMERIC" 
isMandatory="true" length="12"/>
                <db-attribute name="ID_FRUIT_TREES" type="NUMERIC" 
isPrimaryKey="true" isMandatory="true" length="12"/>
                <db-attribute name="PLANTING_DATE" type="DATE" 
isMandatory="true"/>
@@ -24,7 +24,7 @@
                </db-key-generator>
        </db-entity>
        <db-entity name="TREE_TYPES">
-               <db-attribute name="CODE_TYPE_TREE" type="NUMERIC" 
isMandatory="true" length="3"/>
+               <db-attribute name="CODE_TYPE_TREE" type="NUMERIC" 
isPrimaryKey="true" isMandatory="true" length="3"/>
                <db-attribute name="DESCRIPTION_TYPE_TREE" type="VARCHAR" 
length="30"/>
                <db-attribute name="ID_TREE_TYPE" type="NUMERIC" 
isPrimaryKey="true" isMandatory="true" length="12"/>
                <db-key-generator>
@@ -51,7 +51,7 @@
        <db-relationship name="toTreeType" source="FRUIT_TREES" 
target="TREE_TYPES" toMany="false">
                <db-attribute-pair source="CODE_TYPE_TREE" 
target="CODE_TYPE_TREE"/>
        </db-relationship>
-       <db-relationship name="toFruitTree" source="TREE_TYPES" 
target="FRUIT_TREES" toMany="true">
+       <db-relationship name="toFruitTree" source="TREE_TYPES" 
target="FRUIT_TREES" toDependentPK="true" toMany="true">
                <db-attribute-pair source="CODE_TYPE_TREE" 
target="CODE_TYPE_TREE"/>
        </db-relationship>
        <obj-relationship name="toFruitTree" source="Enclosure" 
target="FruitTree" db-relationship-path="toFruitTree"/>
-- 

This fixed the problem. The following SQL is generated in the last commit:

INFO: INSERT INTO "FRUIT_TREES" ("CODE_TYPE_TREE", "ID_ENCLOSURE", 
"ID_FRUIT_TREES", "PLANTING_DATE") VALUES (?, ?, ?, ?)
INFO: [batch bind: 1->CODE_TYPE_TREE:1, 2->ID_ENCLOSURE:200, 
3->ID_FRUIT_TREES:200, 4->PLANTING_DATE:'2012-10-25 22:13:46.071']
                
> FK pointing to an UNIQUE field. Problems to insert registers
> ------------------------------------------------------------
>
>                 Key: CAY-1751
>                 URL: https://issues.apache.org/jira/browse/CAY-1751
>             Project: Cayenne
>          Issue Type: Improvement
>          Components: Core Library, Database integration
>    Affects Versions: 3.0.2
>         Environment: Eclipse + Java 5 + Junit + Cayenne 3.0.2 (Generic 
> Persistent Class) + Oracle 10g Database
>            Reporter: Felipe Martín Santos
>            Priority: Minor
>              Labels: features, patch
>             Fix For: 3.0.2, 3.1 (final)
>
>         Attachments: cay1751.tar.gz, cayenne.xml, FieldDomainMap.map.xml, 
> FieldDomainNode.driver.xml, fruits.sql, FruitTest.java
>
>
> I need insert a new record and the table where insert have 2 FK not null:
> If I relate TABLE_A (FK) with TABLE_B (PK)  Cayenne works well but if the 
> relationship is between TABLE_A (FK)  with TABLE_C (An UNIQUE field), cayenne 
> can´t extract de value of that UNIQUE value, and the insert sentence 
> generated live that field null and I get next error:
> java.sql.SQLIntegrityConstraintViolationException: ORA-01400. Can not perform 
> an insert NULL

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to