Author: awhite Date: Thu Oct 5 12:13:26 2006 New Revision: 453321 URL: http://svn.apache.org/viewvc?view=rev&rev=453321 Log: Tests and fixes for entities with multiple same-typed embedded entities, which themselves have relations to other entities.
Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java (with props) incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java (with props) incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestMultipleSameTypedEmbeddedWithEagerRelations.java Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java?view=diff&rev=453321&r1=453320&r2=453321 ============================================================================== --- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java (original) +++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java Thu Oct 5 12:13:26 2006 @@ -392,8 +392,10 @@ */ private Joins eagerJoin(Joins joins, ClassMapping cls, boolean forceInner) { boolean inverse = field.getJoinDirection() == field.JOIN_INVERSE; - if (!inverse) + if (!inverse) { joins = join(joins, false); + joins = setEmbeddedVariable(joins); + } // and join into relation ForeignKey fk = field.getForeignKey(cls); @@ -404,6 +406,17 @@ field.getSelectSubclasses(), inverse, false); } + /** + * If joining from an embedded owner, use variable to create a unique + * alias in case owner contains other same-typed embedded relations. + */ + private Joins setEmbeddedVariable(Joins joins) { + if (field.getDefiningMetaData().getEmbeddingMetaData() == null) + return joins; + return joins.setVariable(field.getDefiningMetaData(). + getEmbeddingMetaData().getFieldMetaData().getName()); + } + public int select(Select sel, OpenJPAStateManager sm, JDBCStore store, JDBCFetchConfiguration fetch, int eagerMode) { if (field.getJoinDirection() == field.JOIN_INVERSE) @@ -662,6 +675,8 @@ throw RelationStrategies.unjoinable(field); return joins; } + + joins = setEmbeddedVariable(joins); if (forceOuter) return joins.outerJoinRelation(field.getName(), field.getForeignKey(clss[0]), clss[0], Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java?view=auto&rev=453321 ============================================================================== --- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java (added) +++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java Thu Oct 5 12:13:26 2006 @@ -0,0 +1,45 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed 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.Embeddable; +import javax.persistence.CascadeType; +import javax.persistence.ManyToOne; + [EMAIL PROTECTED] +public class EmbeddableWithRelation { + + private String name; + + @ManyToOne(cascade=CascadeType.ALL) + private MultipleSameTypedEmbedded rel; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public MultipleSameTypedEmbedded getRel() { + return rel; + } + + public void setRel(MultipleSameTypedEmbedded rel) { + this.rel = rel; + } +} Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java?view=auto&rev=453321 ============================================================================== --- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java (added) +++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java Thu Oct 5 12:13:26 2006 @@ -0,0 +1,78 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed 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.AttributeOverride; +import javax.persistence.AssociationOverride; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.Version; + [EMAIL PROTECTED] +public class MultipleSameTypedEmbedded { + + @Id + @GeneratedValue + private long id; + + private String name; + + @Embedded + @AttributeOverride(name="name", [EMAIL PROTECTED](name="E1_NAME")) + @AssociationOverride(name="rel", [EMAIL PROTECTED](name="E1_REL")) + private EmbeddableWithRelation embed1; + + @Embedded + @AttributeOverride(name="name", [EMAIL PROTECTED](name="E2_NAME")) + @AssociationOverride(name="rel", [EMAIL PROTECTED](name="E2_REL")) + private EmbeddableWithRelation embed2; + + @Version + private Integer optLock; + + public long getId() { + return id; + } + + public EmbeddableWithRelation getEmbed1() { + return embed1; + } + + public void setEmbed1(EmbeddableWithRelation embed1) { + this.embed1 = embed1; + } + + public EmbeddableWithRelation getEmbed2() { + return embed2; + } + + public void setEmbed2(EmbeddableWithRelation embed2) { + this.embed2 = embed2; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} Propchange: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java ------------------------------------------------------------------------------ svn:executable = * Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestMultipleSameTypedEmbeddedWithEagerRelations.java URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestMultipleSameTypedEmbeddedWithEagerRelations.java?view=auto&rev=453321 ============================================================================== --- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestMultipleSameTypedEmbeddedWithEagerRelations.java (added) +++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestMultipleSameTypedEmbeddedWithEagerRelations.java Thu Oct 5 12:13:26 2006 @@ -0,0 +1,110 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed 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.HashMap; +import java.util.List; +import java.util.Map; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import javax.persistence.Query; + +import junit.framework.TestCase; +import junit.textui.TestRunner; + +/** + * Test that querying and retrieving entities with multiple same-typed embedded + * relations, which themselves have eager relations, works. + * + * @author Abe White + */ +public class TestMultipleSameTypedEmbeddedWithEagerRelations + extends TestCase { + + private EntityManagerFactory emf; + + public void setUp() { + Map props = new HashMap(); + props.put("openjpa.MetaDataFactory", "jpa(Types=" + + EmbeddableWithRelation.class.getName() + ";" + + MultipleSameTypedEmbedded.class.getName() + ")"); + emf = Persistence.createEntityManagerFactory("test", props); + + EmbeddableWithRelation embed1 = new EmbeddableWithRelation(); + embed1.setName("embed1"); + EmbeddableWithRelation embed2 = new EmbeddableWithRelation(); + embed2.setName("embed2"); + + MultipleSameTypedEmbedded m1 = new MultipleSameTypedEmbedded(); + m1.setName("m1"); + m1.setEmbed1(embed1); + m1.setEmbed2(embed2); + MultipleSameTypedEmbedded m2 = new MultipleSameTypedEmbedded(); + m2.setName("m2"); + m2.setEmbed1(embed2); + m2.setEmbed2(embed1); + + embed1.setRel(m1); + embed2.setRel(m2); + + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.persist(m1); + em.persist(m2); + em.getTransaction().commit(); + em.close(); + } + + public void tearDown() { + if (emf == null) + return; + try { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + em.createQuery("delete from MultipleSameTypedEmbedded"). + executeUpdate(); + em.getTransaction().commit(); + em.close(); + emf.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void testQuery() { + EntityManager em = emf.createEntityManager(); + Query q = em.createQuery("select m from MultipleSameTypedEmbedded m " + + "where m.embed1.rel.name = 'm1' " + + "and m.embed2.rel.name = 'm2'"); + List res = q.getResultList(); + assertEquals(1, res.size()); + + MultipleSameTypedEmbedded m = (MultipleSameTypedEmbedded) res.get(0); + assertEquals("m1", m.getName()); + assertEquals("embed1", m.getEmbed1().getName()); + assertEquals("m1", m.getEmbed1().getRel().getName()); + assertEquals("embed2", m.getEmbed2().getName()); + assertEquals("m2", m.getEmbed2().getRel().getName()); + + em.close(); + } + + public static void main(String[] args) { + TestRunner.run(TestMultipleSameTypedEmbeddedWithEagerRelations.class); + } +} +