You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pc...@apache.org on 2007/06/08 03:18:41 UTC

svn commit: r545360 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel: EntityB.java EntityC.java TestNoForeignKeyViolation.java

Author: pcl
Date: Thu Jun  7 18:18:40 2007
New Revision: 545360

URL: http://svn.apache.org/viewvc?view=rev&rev=545360
Log:
Reece's test case from OPENJPA-235. I was a bit nervous about the changes to EntityB and EntityC's relationships, but all the existing OpenJPA tests pass, so evidently we did not rely on the particular settings as they were.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/TestNoForeignKeyViolation.java
Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityB.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityC.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityB.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityB.java?view=diff&rev=545360&r1=545359&r2=545360
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityB.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityB.java Thu Jun  7 18:18:40 2007
@@ -40,7 +40,7 @@
 
     private String name;
 
-    @OneToOne(cascade = CascadeType.ALL, optional = false)
+    @OneToOne(cascade = CascadeType.ALL)
     @JoinColumn(name = "entityc_id", referencedColumnName = "entityc_id",
         nullable = false)
     @ForeignKey

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityC.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityC.java?view=diff&rev=545360&r1=545359&r2=545360
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityC.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/EntityC.java Thu Jun  7 18:18:40 2007
@@ -41,7 +41,7 @@
 
     private String name;
 
-    @OneToOne(cascade = CascadeType.ALL, optional = false)
+    @OneToOne(cascade = CascadeType.ALL)
     @JoinColumn(name = "entityd_id", referencedColumnName = "entityd_id",
         nullable = false)
     @ForeignKey

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/TestNoForeignKeyViolation.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/TestNoForeignKeyViolation.java?view=auto&rev=545360
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/TestNoForeignKeyViolation.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/kernel/TestNoForeignKeyViolation.java Thu Jun  7 18:18:40 2007
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openjpa.jdbc.kernel;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test that sql statements get flushed in an order which does not violate
+ * non-nullable foreign key constraints on inserts and deletes.
+ *
+ * @author Reece Garrett
+ */
+public class TestNoForeignKeyViolation
+    extends SingleEMFTestCase {
+
+    private EntityA entityA;
+    private EntityB entityB;
+    private EntityC entityC;
+
+    public void setUp() {
+        setUp(EntityA.class, EntityB.class, EntityC.class, EntityD.class);
+
+        entityA = new EntityA();
+        entityC = new EntityC();
+        EntityD entityD = new EntityD();
+        entityA.setName("entityA");
+        entityB = new EntityB();
+        entityB.setName("entityB");
+        entityC.setName("entityC");
+        entityD.setName("entityD");
+        entityA.setEntityB(entityB);
+        entityB.setEntityC(entityC);
+        entityC.setEntityD(entityD);
+    }
+
+    public void testSqlOrder() {
+
+        EntityManager em = emf.createEntityManager();
+        try {
+            em.getTransaction().begin();
+            em.persist(entityA);
+            em.getTransaction().commit();
+
+            EntityD newEntityD = new EntityD();
+            newEntityD.setName("newEntityD");
+            entityC.setEntityD(newEntityD);
+
+            em.getTransaction().begin();
+            em.merge(entityC);
+            em.getTransaction().commit();
+
+            EntityC newEntityC = new EntityC();
+            newEntityC.setName("newEntityC");
+            newEntityD = new EntityD();
+            newEntityD.setName("newEntityD");
+            newEntityC.setEntityD(newEntityD);
+            entityB.setEntityC(newEntityC);
+
+            em.getTransaction().begin();
+            em.merge(entityB);
+            em.getTransaction().commit();
+        }
+        finally {
+            if (em.getTransaction().isActive())
+                em.getTransaction().rollback();
+            em.close();
+        }
+    }
+}