Author: rpalache
Date: Thu Oct 27 16:42:11 2011
New Revision: 1189858
URL: http://svn.apache.org/viewvc?rev=1189858&view=rev
Log:
OPENJPA-2066 Edge case in OPENJPA-1227. openJPA fails to create a join and thus
returns wrong data when a collection is selected from superclass and another
field is selected from subclass two or more levels down the hierarchy
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildChildClass.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildClass.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/GrandChildClass.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ParentClass.java
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestJoinedMultiInheritanceHierarchy.java
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
Modified:
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java?rev=1189858&r1=1189857&r2=1189858&view=diff
==============================================================================
---
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
(original)
+++
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreManager.java
Thu Oct 27 16:42:11 2011
@@ -1382,7 +1382,7 @@ public class JDBCStoreManager implements
// in certain circumstances force join to superclass table to avoid
// SQL generation error.
- if ( eagerToMany != null && pseld < 0 && seld > 0 && !joined
+ if ( eagerToMany != null && pseld < 0 && !joined
&& parent != null ) {
FieldMapping[] pfms = parent.getDefinedFieldMappings();
for (int i = 0; i < pfms.length; i++) {
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildChildClass.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildChildClass.java?rev=1189858&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildChildClass.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildChildClass.java
Thu Oct 27 16:42:11 2011
@@ -0,0 +1,51 @@
+/*
+ * 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.*;
+import org.apache.openjpa.persistence.FetchAttribute;
+import org.apache.openjpa.persistence.FetchGroup;
+import org.apache.openjpa.persistence.FetchGroups;
+
+@Entity
+@Inheritance(strategy=InheritanceType.JOINED)
+public class ChildChildClass extends ChildClass {
+
+ @Basic(fetch=FetchType.LAZY)
+ private String name2;
+
+ public ChildChildClass() {
+ super();
+ }
+
+ public String getName2() {
+ return name2;
+ }
+
+ public void setName2(String name2) {
+ this.name2 = name2;
+ }
+
+ @Override
+ public String toString() {
+ return super.toString()+"\n"+"Name2: "+name2+"\n";
+ }
+}
+
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildClass.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildClass.java?rev=1189858&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildClass.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ChildClass.java
Thu Oct 27 16:42:11 2011
@@ -0,0 +1,64 @@
+/*
+ * 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.*;
+import org.apache.openjpa.persistence.FetchAttribute;
+import org.apache.openjpa.persistence.FetchGroup;
+import org.apache.openjpa.persistence.FetchGroups;
+
+@Entity
+@Inheritance(strategy=InheritanceType.JOINED)
+
+public class ChildClass extends ParentClass {
+
+ @Basic(fetch=FetchType.LAZY)
+ private String name;
+
+ public ChildClass() {
+ super();
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder sBuf = new StringBuilder();
+ sBuf.append("Name: ").append(name).append("\n");
+ sBuf.append("Items: ");
+ if (getItems().isEmpty()) {
+ sBuf.append("none\n");
+ } else {
+ sBuf.append(getItems().size()).append('\n');
+ for (String item : getItems()) {
+ sBuf.append("\t");
+ sBuf.append(item);
+ sBuf.append("\n");
+ }
+ }
+ return sBuf.toString();
+ }
+}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/GrandChildClass.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/GrandChildClass.java?rev=1189858&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/GrandChildClass.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/GrandChildClass.java
Thu Oct 27 16:42:11 2011
@@ -0,0 +1,39 @@
+/*
+ * 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.*;
+import org.apache.openjpa.persistence.PersistentCollection;
+
+@Entity
+@Inheritance(strategy=InheritanceType.JOINED)
+public class GrandChildClass extends ChildChildClass {
+
+ @Basic(fetch=FetchType.EAGER)
+ private String someUnloadedField;
+
+ public String getSomeUnloadedString() {
+ return someUnloadedField;
+ }
+
+ public String toString() {
+ return super.toString() + "SomeUnloadedField: " + someUnloadedField +
"\n";
+ }
+}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ParentClass.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ParentClass.java?rev=1189858&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ParentClass.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/ParentClass.java
Thu Oct 27 16:42:11 2011
@@ -0,0 +1,50 @@
+/*
+ * 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 java.util.Collection;
+
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+
+import org.apache.openjpa.persistence.PersistentCollection;
+
+@Entity
+@Inheritance(strategy=InheritanceType.JOINED)
+public abstract class ParentClass {
+
+ @PersistentCollection(fetch=FetchType.EAGER)
+ private Set<String> items = new HashSet<String>();
+
+ protected ParentClass() {
+ }
+
+ public Set<String> getItems() {
+ return items;
+ }
+
+ public void setItems(Collection<String> items) {
+ this.items.addAll(items);
+ }
+}
Added:
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestJoinedMultiInheritanceHierarchy.java
URL:
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestJoinedMultiInheritanceHierarchy.java?rev=1189858&view=auto
==============================================================================
---
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestJoinedMultiInheritanceHierarchy.java
(added)
+++
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestJoinedMultiInheritanceHierarchy.java
Thu Oct 27 16:42:11 2011
@@ -0,0 +1,66 @@
+/*
+ * 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.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Collection;
+
+import org.apache.openjpa.meta.FetchGroup;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.PersistenceException;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestJoinedMultiInheritanceHierarchy
+ extends SingleEMFTestCase {
+
+ public void setUp() {
+ super.setUp( CLEAR_TABLES, ChildChildClass.class, ChildClass.class,
+ GrandChildClass.class, ParentClass.class,
+ "openjpa.BrokerImpl", "EvictFromDataCache=true" );
+ }
+
+ public void testCacheSqlGeneration() throws PersistenceException {
+ OpenJPAEntityManager em = emf.createEntityManager();
+
+ em.getTransaction().begin();
+ final GrandChildClass notEmpty = new GrandChildClass();
+ notEmpty.setName("Not empty object");
+ Collection<String> itemSet = notEmpty.getItems();
+ for (int i = 0; i < 5; i++) {
+ itemSet.add(notEmpty.getName() + " : item n." + i);
+ }
+
+ notEmpty.setItems(itemSet);
+
+ final GrandChildClass empty = new GrandChildClass();
+ empty.setName("empty object");
+
+ em.persist(notEmpty);
+ em.persist(empty);
+ em.getTransaction().commit();
+
+ em.evictAll();
+ assertTrue(empty.getItems().isEmpty());
+ }
+
+}
+