diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java index 8e9bbf19b..e2ff5ed96 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java @@ -392,8 +392,10 @@ public class RelationFieldStrategy */ 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 @@ public class RelationFieldStrategy 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 @@ public class RelationFieldStrategy throw RelationStrategies.unjoinable(field); return joins; } + + joins = setEmbeddedVariable(joins); if (forceOuter) return joins.outerJoinRelation(field.getName(), field.getForeignKey(clss[0]), clss[0], diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java new file mode 100755 index 000000000..3c70b1334 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/EmbeddableWithRelation.java @@ -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; + +@Embeddable +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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java new file mode 100755 index 000000000..4cb9416f4 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/MultipleSameTypedEmbedded.java @@ -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; + +@Entity +public class MultipleSameTypedEmbedded { + + @Id + @GeneratedValue + private long id; + + private String name; + + @Embedded + @AttributeOverride(name="name", column=@Column(name="E1_NAME")) + @AssociationOverride(name="rel", joinColumns=@JoinColumn(name="E1_REL")) + private EmbeddableWithRelation embed1; + + @Embedded + @AttributeOverride(name="name", column=@Column(name="E2_NAME")) + @AssociationOverride(name="rel", joinColumns=@JoinColumn(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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestMultipleSameTypedEmbeddedWithEagerRelations.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestMultipleSameTypedEmbeddedWithEagerRelations.java new file mode 100644 index 000000000..2004adf1e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/relations/TestMultipleSameTypedEmbeddedWithEagerRelations.java @@ -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); + } +} +