Author: jgrassel
Date: Mon Aug 25 19:45:38 2014
New Revision: 1620420

URL: http://svn.apache.org/r1620420
Log:
OPENJPA-2525: Use of JoinColumn targets to another joinColumn key exposed as an 
attribute will cause a ConstrainViolation exception on persist

Added:
    
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ACase.java
   (with props)
    
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AEvident.java
   (with props)
    
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AText.java
   (with props)
    
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestO2ORefColumn.java
   (with props)

Added: 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ACase.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ACase.java?rev=1620420&view=auto
==============================================================================
--- 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ACase.java
 (added)
+++ 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ACase.java
 Mon Aug 25 19:45:38 2014
@@ -0,0 +1,61 @@
+/*
+ * 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.persistence.relations;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+@Entity
+public class ACase {
+
+       private int id;
+       @Id
+       @GeneratedValue(strategy=GenerationType.IDENTITY)
+       public int getId() {
+               return id;
+       }
+
+       public void setId(int id) {
+               this.id = id;
+       }
+
+    private String name;
+    public String getName() {
+       return this.name;
+    }
+
+    public void setName(String name) {
+       this.name = name;
+    }
+
+       private AText aText;
+       @OneToOne(fetch=FetchType.LAZY, mappedBy="aCase", 
cascade=CascadeType.MERGE)
+       public AText getAText() {
+               return aText;
+       }
+
+       public void setAText(AText aText) {
+               this.aText = aText;
+       }
+}

Propchange: 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ACase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AEvident.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AEvident.java?rev=1620420&view=auto
==============================================================================
--- 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AEvident.java
 (added)
+++ 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AEvident.java
 Mon Aug 25 19:45:38 2014
@@ -0,0 +1,63 @@
+/*
+ * 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.persistence.relations;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+
+@Entity
+public class AEvident {
+
+       private int id;
+       @Id
+       @GeneratedValue(strategy=GenerationType.IDENTITY)
+       public int getId() {
+               return id;
+       }
+
+       public void setId(int id) {
+               this.id = id;
+       }
+
+    private String name;
+    public String getName() {
+       return this.name;
+    }
+
+    public void setName(String name) {
+       this.name = name;
+    }
+
+       private AText aText;
+       @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
+       @JoinColumn(name="ACASE_ID", referencedColumnName="ACASE_ID")
+       public AText getAText() {
+               return aText;
+       }
+
+       public void setAText(AText aText) {
+               this.aText = aText;
+       }
+}

Propchange: 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AEvident.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AText.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AText.java?rev=1620420&view=auto
==============================================================================
--- 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AText.java
 (added)
+++ 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AText.java
 Mon Aug 25 19:45:38 2014
@@ -0,0 +1,90 @@
+/*
+ * 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.persistence.relations;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
+
+@Entity
+public class AText {
+
+       private int id;
+       @Id
+       @GeneratedValue(strategy=GenerationType.IDENTITY)
+       public int getId() {
+               return id;
+       }
+
+       public void setId(int id) {
+               this.id = id;
+       }
+
+    private String name;
+    public String getName() {
+       return this.name;
+    }
+
+    public void setName(String name) {
+       this.name = name;
+    }
+
+       private ACase aCase;
+       @OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
+       @JoinColumn(name="ACASE_ID", nullable=false)
+       public ACase getACase() {
+               return aCase;
+       }
+
+       public void setACase(ACase aCase) {
+               this.aCase = aCase;
+       }
+
+
+       private Set<AEvident> aEvidents = new HashSet<AEvident>();
+       @OneToMany(targetEntity=AEvident.class, mappedBy="aText", 
cascade=CascadeType.MERGE)
+       public Set<AEvident> getAEvidents() {
+               return aEvidents;
+       }
+
+       public void setAEvidents(Set<AEvident> aEvidents) {
+               this.aEvidents = aEvidents;
+       }
+
+       private int aCaseId;
+       @Column(name="ACASE_ID", insertable=false, updatable=false, unique=true)
+       public int getACaseId() {
+           return aCaseId;
+       }
+
+       public void setACaseId(int aCaseId) {
+           this.aCaseId = aCaseId;
+       }
+
+}

Propchange: 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/AText.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestO2ORefColumn.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestO2ORefColumn.java?rev=1620420&view=auto
==============================================================================
--- 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestO2ORefColumn.java
 (added)
+++ 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestO2ORefColumn.java
 Mon Aug 25 19:45:38 2014
@@ -0,0 +1,103 @@
+/*
+ * 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.persistence.relations;
+
+import javax.persistence.EntityManager;
+
+import junit.framework.Assert;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Unit test to verify the foreign key of a Join column in an association 
(aText), that is exposed as an
+ * attribute, is updated after the entity is flushed or committed to the data 
store.
+ * See AText -> ACase's foreign key also exposed a non-insertable and 
non-updatable attribute 'aCaseId'.
+ *
+ * In the test case, if the aCaseId is not updated with the foreign key 
(aCase.id) value, an association
+ * of aText (aEvident) with JoinColumn(..referencedColumnName=..) overridden 
to non-standard foreign key,
+ * a constraint violation will occur when aEvident is persisted to the data 
store.
+ */
+public class TestO2ORefColumn extends SingleEMFTestCase {
+
+    public void setUp () {
+        setUp(CLEAR_TABLES,
+                ACase.class, AText.class, AEvident.class,
+                "openjpa.jdbc.MappingDefaults", 
"ForeignKeyDeleteAction=cascade,JoinForeignKeyDeleteAction=cascade"
+                );
+    }
+
+    public void testRefColumnJoinEntities () {
+        AEvident aEvident = new AEvident();
+        aEvident.setName("Evident_A");
+
+        AText aText = new AText();
+        aText.setName("Text_A");
+        aText.getAEvidents().add(aEvident);
+        aEvident.setAText(aText);
+
+        ACase aCase = new ACase();
+        aCase.setName ("Case_A");
+        aCase.setAText(aText);
+        aText.setACase(aCase);
+
+        EntityManager em = emf.createEntityManager ();
+        em.getTransaction().begin ();
+        em.persist(aEvident);
+        em.persist(aText);
+        em.persist(aCase);
+        em.getTransaction ().commit ();
+
+        verify(aCase, aText, aEvident);
+
+        em.clear();
+
+        ACase fACase = em.find(ACase.class, aCase.getId());
+        AText fAText = fACase.getAText();
+        AEvident fAEvident = fAText.getAEvidents().iterator().next();
+        verify(fACase, fAText, fAEvident);
+
+        em.close ();
+    }
+
+    private void verify(ACase aCase, AText aText, AEvident aEvident) {
+        Assert.assertNotNull(aCase);
+        Assert.assertNotNull(aText);
+        Assert.assertNotNull(aEvident);
+
+        Assert.assertTrue(aCase.getId() != 0);
+        Assert.assertTrue(aText.getId() != 0);
+        Assert.assertTrue(aEvident.getId() != 0);
+
+        Assert.assertEquals("Case_A", aCase.getName());
+        Assert.assertEquals("Text_A", aText.getName());
+        Assert.assertEquals("Evident_A", aEvident.getName());
+
+        Assert.assertNotNull(aCase.getAText());
+        Assert.assertSame(aCase.getAText(), aText);
+        Assert.assertNotNull(aText.getACase());
+        Assert.assertSame(aCase, aText.getACase());
+
+        Assert.assertEquals(aText.getACaseId(), aCase.getId());
+        Assert.assertNotNull(aText.getAEvidents());
+        Assert.assertTrue(aText.getAEvidents().iterator().hasNext());
+        Assert.assertSame(aEvident, aText.getAEvidents().iterator().next());
+        Assert.assertNotNull(aEvident.getAText());
+        Assert.assertSame(aText, aEvident.getAText());
+    }
+}

Propchange: 
openjpa/branches/2.2.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestO2ORefColumn.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to