You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by fa...@apache.org on 2008/12/04 23:40:45 UTC

svn commit: r723477 - in /openjpa/branches/1.3.x: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/

Author: faywang
Date: Thu Dec  4 14:40:45 2008
New Revision: 723477

URL: http://svn.apache.org/viewvc?rev=723477&view=rev
Log:
OPENJPA-815: retrieval fails for an entity that contains an
embeddable, which contains a toMany relation.

Added:
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_Embed_ToMany.java
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_ToMany.java
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_Embed_ToMany.java
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java
Modified:
    openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java

Modified: openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java?rev=723477&r1=723476&r2=723477&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java (original)
+++ openjpa/branches/1.3.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StoreCollectionFieldStrategy.java Thu Dec  4 14:40:45 2008
@@ -594,13 +594,21 @@
             
             ClassMapping mapping = field.getDefiningMapping();
             Object oid = sm.getObjectId();
-            Column[] cols = mapping.getPrimaryKeyColumns();
+            
             if (sel == null)
                 sel = ((LogicalUnion.UnionSelect)union.getSelects()[0]).
                 getDelegate();
+           
+            ValueMapping embed = mapping.getEmbeddingMapping();
+            if (embed != null) 
+                mapping = getOwner(embed);
+
+            if (mapping != null) {
+                Column[] cols = mapping.getPrimaryKeyColumns();
+                sel.wherePrimaryKey(mapping, cols, cols, oid, store, 
+                    null, null, parmList);
+            }
 
-            sel.wherePrimaryKey(mapping, cols, cols, oid, store, 
-                	null, null, parmList);
             List nonFKParams = sel.getSQL().getNonFKParameters();
             if (nonFKParams != null && nonFKParams.size() > 0) 
                 parmList.addAll(nonFKParams);
@@ -641,6 +649,16 @@
         else
             sm.storeObject(field.getIndex(), coll);
     }
+    
+    private ClassMapping getOwner(ValueMapping embed) {
+        if (embed == null)
+            return null;
+        ClassMapping owner = embed.getFieldMapping().getDefiningMapping();
+        if (owner.getPrimaryKeyFields().length > 0)
+            return owner;
+        ValueMapping embed1 = (ValueMapping) owner.getEmbeddingMetaData();
+        return getOwner(embed1);
+    }
 
     protected Union newUnion(final OpenJPAStateManager sm, final JDBCStore store,
         final JDBCFetchConfiguration fetch, final ClassMapping[] elems,

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_Embed_ToMany.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_Embed_ToMany.java?rev=723477&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_Embed_ToMany.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_Embed_ToMany.java Thu Dec  4 14:40:45 2008
@@ -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.embed;
+
+import javax.persistence.Embeddable;
+import javax.persistence.Embedded;
+
+@Embeddable 
+public class Embed_Embed_ToMany {
+    protected int intVal1;
+    protected int intVal2;
+    protected int intVal3;
+    @Embedded
+    protected Embed_ToMany embed;
+    
+    public int getIntVal1() {
+        return intVal1;
+    }
+    
+    public void setIntVal1(int intVal1) {
+        this.intVal1 = intVal1;
+    }
+    
+    public int getIntVal2() {
+        return intVal2;
+    }
+    
+    public void setIntVal2(int intVal2) {
+        this.intVal2 = intVal2;
+    }
+    
+    public int getIntVal3() {
+        return intVal3;
+    }
+    
+    public void setIntVal3(int intVal3) {
+        this.intVal3 = intVal3;
+    }
+    
+    public Embed_ToMany getEmbed() {
+        return embed;
+    }
+ 
+    public void setEmbed(Embed_ToMany embed) {
+        this.embed = embed;
+    }
+    
+}

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_ToMany.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_ToMany.java?rev=723477&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_ToMany.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_ToMany.java Thu Dec  4 14:40:45 2008
@@ -0,0 +1,68 @@
+/*
+ * 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.embed;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.Embeddable;
+import javax.persistence.FetchType;
+import javax.persistence.OneToMany;
+
+@Embeddable 
+public class Embed_ToMany {
+    protected String name1;
+    protected String name2;
+    protected String name3;
+    
+    @OneToMany(fetch=FetchType.EAGER)
+    public List<EntityB1> bs = new ArrayList<EntityB1>();
+    
+    public String getName1() {
+        return name1;
+    }
+    
+    public void setName1(String name1) {
+        this.name1 = name1;
+    }
+    
+    public String getName2() {
+        return name2;
+    }
+    
+    public void setName2(String name2) {
+        this.name2 = name2;
+    }
+    
+    public String getName3() {
+        return name3;
+    }
+    
+    public void setName3(String name3) {
+        this.name3 = name3;
+    }
+    
+    public List<EntityB1> getEntityBs() {
+        return bs;
+    }
+ 
+    public void addEntityB(EntityB1 b) {
+        bs.add(b);
+    }
+}

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_Embed_ToMany.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_Embed_ToMany.java?rev=723477&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_Embed_ToMany.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_Embed_ToMany.java Thu Dec  4 14:40:45 2008
@@ -0,0 +1,75 @@
+/*
+ * 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.embed;
+
+import java.io.Serializable;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Embedded;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Id;
+
+@Entity
+public class EntityA_Embed_Embed_ToMany implements Serializable {
+    @Id
+    Integer id;
+
+    @Column(length = 30)
+    String name;
+
+    @Basic(fetch = FetchType.LAZY)
+    int age;
+
+    @Embedded
+    protected Embed_Embed_ToMany embed;
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Embed_Embed_ToMany getEmbed() {
+        return embed;
+    }
+
+    public void setEmbed(Embed_Embed_ToMany embed) {
+        this.embed = embed;
+    }
+}

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java?rev=723477&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java Thu Dec  4 14:40:45 2008
@@ -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.embed;
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class EntityB1 implements Serializable {
+
+    @Id
+    int id;
+
+    @Column(length=30)
+    String name;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}
+

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java?rev=723477&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java Thu Dec  4 14:40:45 2008
@@ -0,0 +1,158 @@
+/*
+ * 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.embed;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestEmbeddable extends SingleEMFTestCase {
+    public int ID = 1;
+
+    public void setUp() {
+        setUp(Embed_Embed_ToMany.class, Embed_ToMany.class, 
+            EntityA_Embed_Embed_ToMany.class, EntityB1.class, 
+            CLEAR_TABLES);
+    }
+
+    public void testEntityA_Embed_Embed_ToMany() {
+        createEntityA_Embed_Embed_ToMany();
+        queryEntityA_Embed_Embed_ToMany();
+        findEntityA_Embed_Embed_ToMany();
+    }
+
+    public Embed_ToMany createEmbed_ToMany(EntityManager em, int id) {
+        Embed_ToMany embed = new Embed_ToMany();
+        embed.setName1("name1");
+        embed.setName2("name2");
+        embed.setName3("name3");
+        for (int i = 0; i < 1; i++) {
+            EntityB1 b = new EntityB1();
+            b.setId(id + i);
+            b.setName("b" + id + i);
+            embed.addEntityB(b);
+            em.persist(b);
+        }
+        return embed;
+    }
+
+   /*
+     * Create EntityA_Embed_Embed_ToMany
+     */
+    public void createEntityA_Embed_Embed_ToMany() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        createEntityA_Embed_Embed_ToMany(em, ID);
+        tran.begin();
+        em.flush();
+        tran.commit();
+        em.close();
+    }
+
+    public void createEntityA_Embed_Embed_ToMany(EntityManager em, int id) {
+        EntityA_Embed_Embed_ToMany a = new EntityA_Embed_Embed_ToMany();
+        a.setId(id);
+        a.setName("a" + id);
+        a.setAge(id);
+        Embed_Embed_ToMany embed = createEmbed_Embed_ToMany(em, id);
+        a.setEmbed(embed);
+        em.persist(a);
+    }
+
+    public Embed_Embed_ToMany createEmbed_Embed_ToMany(EntityManager em, int id) {
+        Embed_Embed_ToMany embed = new Embed_Embed_ToMany();
+        embed.setIntVal1(1);
+        embed.setIntVal2(2);
+        embed.setIntVal3(3);
+        Embed_ToMany embed_ToMany = createEmbed_ToMany(em, id);
+        embed.setEmbed(embed_ToMany);
+        return embed;
+    }
+    
+    /*
+     * Find EntityA_Embed_Embed_ToMany
+     */
+    public void findEntityA_Embed_Embed_ToMany() {
+        EntityManager em = emf.createEntityManager();
+        EntityA_Embed_Embed_ToMany a = em.find(EntityA_Embed_Embed_ToMany.class, ID);
+        checkEntityA_Embed_Embed_ToMany(a);
+        em.close();
+    }
+
+    public void checkEmbed_ToMany(Embed_ToMany embed) {
+        String name1 = embed.getName1();
+        String name2 = embed.getName2();
+        String name3 = embed.getName3();
+        assertEquals("name1", name1);
+        assertEquals("name2", name2);
+        assertEquals("name3", name3);
+        List<EntityB1> bs = embed.getEntityBs();
+        for (EntityB1 b : bs) {
+            assertEquals(1, b.getId());
+            assertEquals("b" + b.getId() + "0", b.getName());
+        }
+    }
+
+    /*
+     * check EntityA_Embed_Embed_ToMany
+     */
+    public void checkEntityA_Embed_Embed_ToMany(EntityA_Embed_Embed_ToMany a) {
+        int id = a.getId();
+        String name = a.getName();
+        int age = a.getAge();
+        assertEquals(1, id);
+        assertEquals("a" + id ,name);
+        assertEquals(1, age);
+        Embed_Embed_ToMany embed = a.getEmbed();
+        checkEmbed_Embed_ToMany(embed);
+    }
+    
+    public void checkEmbed_Embed_ToMany(Embed_Embed_ToMany embed) {
+        int intVal1 = embed.getIntVal1();
+        int intVal2 = embed.getIntVal2();
+        int intVal3 = embed.getIntVal3();
+        assertEquals(1, intVal1);
+        assertEquals(2, intVal2);
+        assertEquals(3, intVal3);
+        Embed_ToMany embed1 = embed.getEmbed();
+        checkEmbed_ToMany(embed1);
+    }
+    
+    /*
+     * Query EntityA_Embed_Embed_ToMany
+     */
+    public void queryEntityA_Embed_Embed_ToMany() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        tran.begin();
+        Query q = em.createQuery("select a from EntityA_Embed_Embed_ToMany a");
+        List<EntityA_Embed_Embed_ToMany> as = q.getResultList();
+        for (EntityA_Embed_Embed_ToMany a : as) {
+            checkEntityA_Embed_Embed_ToMany(a);
+        }
+        tran.commit();
+        em.close();
+    }
+
+
+}