From a06d373573b5e12ba309ef9e6137ee4e759eae5e Mon Sep 17 00:00:00 2001 From: Albert Lee Date: Mon, 1 Oct 2012 22:15:42 +0000 Subject: [PATCH] OPENJPA-2247 Complete fix for 1x1 bi and mx1 uni relationships of the described problem. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1392661 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/meta/MappingRepository.java | 14 +++++-- .../org/apache/openjpa/meta/PChildBi.java | 42 +++++++++++++++++++ .../java/org/apache/openjpa/meta/Parent.java | 8 ++++ .../openjpa/meta/TestSecondaryTable.java | 11 ++++- 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/PChildBi.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java index 2d65c97f1..e5766c823 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java @@ -88,7 +88,6 @@ import org.apache.openjpa.jdbc.sql.JoinSyntaxes; import org.apache.openjpa.lib.conf.Configurable; import org.apache.openjpa.lib.conf.Configurations; import org.apache.openjpa.lib.util.J2DoPrivHelper; -import org.apache.openjpa.lib.util.JavaVersions; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.meta.FieldMetaData; @@ -1084,7 +1083,10 @@ public class MappingRepository extends MetaDataRepository { field.getAssociationType() == FieldMetaData.MANY_TO_ONE && hasJoinTable(field) && !isBidirectional(field)) { - field.getValueMapping().getValueInfo().setColumns(field.getElementMapping().getValueInfo().getColumns()); + List cols = field.getElementMapping().getValueInfo().getColumns(); + if (cols != null && cols.size() > 0) { + field.getValueMapping().getValueInfo().setColumns(cols); + } return true; } return false; @@ -1096,8 +1098,9 @@ public class MappingRepository extends MetaDataRepository { hasJoinTable(field) && !isBidirectional(field)) { List cols = field.getElementMapping().getValueInfo().getColumns(); - if (cols != null && cols.size() > 0) + if (cols != null && cols.size() > 0) { field.getValueMapping().getValueInfo().setColumns(cols); + } return true; } return false; @@ -1108,7 +1111,10 @@ public class MappingRepository extends MetaDataRepository { field.getAssociationType() == FieldMetaData.ONE_TO_ONE && hasJoinTable(field) && isBidirectional(field)) { - field.getValueMapping().getValueInfo().setColumns(field.getElementMapping().getValueInfo().getColumns()); + List cols = field.getElementMapping().getValueInfo().getColumns(); + if (cols != null && cols.size() > 0) { + field.getValueMapping().getValueInfo().setColumns(cols); + } return true; } return false; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/PChildBi.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/PChildBi.java new file mode 100644 index 000000000..7ee128d21 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/PChildBi.java @@ -0,0 +1,42 @@ +/* + * 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.meta; + +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Version; + +@Entity +public class PChildBi { + @Id + @GeneratedValue + int idChild; + + @Version + int version; + + @Basic + String basic; + + @OneToOne + Parent parent; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/Parent.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/Parent.java index 23698a782..39945b470 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/Parent.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/Parent.java @@ -22,6 +22,7 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.SecondaryTable; @@ -41,4 +42,11 @@ public class Parent { @JoinColumn(name = "CHILD_REF", table = "ParentSecondaryTable", referencedColumnName = "idChild") PChild child; + @OneToOne + @JoinColumn(name = "CHILDBI_REF", table = "ParentSecondaryTable", referencedColumnName = "idChild") + PChildBi childbi; + + @ManyToOne + @JoinColumn(name = "CHILDREN_REF", table = "ParentSecondaryTable", referencedColumnName = "idChild") + PChild children; } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/TestSecondaryTable.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/TestSecondaryTable.java index f0475b118..0eec928d2 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/TestSecondaryTable.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/meta/TestSecondaryTable.java @@ -23,7 +23,7 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; public class TestSecondaryTable extends SingleEMFTestCase { public void setUp() { - setUp(Parent.class, PChild.class + setUp(Parent.class, PChild.class, PChildBi.class // Hard code to 2.0 p.xml value. If the p.xml is 1.0, this value will be changed to false, and the test // won't fail. , "openjpa.Compatibility", "NonDefaultMappingAllowed=true"); @@ -36,6 +36,13 @@ public class TestSecondaryTable extends SingleEMFTestCase { FieldMapping fm = getMapping(Parent.class).getFieldMapping("child"); assertNotNull(fm); assertEquals("CHILD_REF", fm.getColumns()[0].getIdentifier().getName()); - } + fm = getMapping(Parent.class).getFieldMapping("childbi"); + assertNotNull(fm); + assertEquals("CHILDBI_REF", fm.getColumns()[0].getIdentifier().getName()); + + fm = getMapping(Parent.class).getFieldMapping("children"); + assertNotNull(fm); + assertEquals("CHILDREN_REF", fm.getColumns()[0].getIdentifier().getName()); + } }