openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r763427 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/
Date Wed, 08 Apr 2009 22:06:57 GMT
Author: faywang
Date: Wed Apr  8 22:06:56 2009
New Revision: 763427

URL: http://svn.apache.org/viewvc?rev=763427&view=rev
Log:
OPENJPA-1028: fixed setMappedByValue when embeddable is involved.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_MappedToOne.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_MappedToOne.java
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java?rev=763427&r1=763426&r2=763427&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java
Wed Apr  8 22:06:56 2009
@@ -54,7 +54,6 @@
 import org.apache.openjpa.jdbc.sql.SelectExecutor;
 import org.apache.openjpa.jdbc.sql.Union;
 import org.apache.openjpa.kernel.OpenJPAStateManager;
-import org.apache.openjpa.kernel.StateManagerImpl;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.ClassMetaData;
@@ -660,7 +659,7 @@
             // By saving the mapped-by info in 'res' is to
             // avoid unneeded SQL pushdown that would otherwise gets
             // generated.
-            if (decMeta != null) {
+            if (decMeta != null && !sm.isEmbedded()) {
         	    mappedByValue = sm.getPersistenceCapable();
         	    res.setMappedByFieldMapping(mappedByFieldMapping);
         	    res.setMappedByValue(mappedByValue);

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_MappedToOne.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_MappedToOne.java?rev=763427&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_MappedToOne.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/Embed_MappedToOne.java
Wed Apr  8 22:06:56 2009
@@ -0,0 +1,65 @@
+/*
+ * 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.OneToOne;
+
+@Embeddable 
+public class Embed_MappedToOne {
+    protected String name1;
+    protected String name2;
+    protected String name3;
+    
+    @OneToOne(mappedBy="entityA")
+    protected EntityB1 bm;
+    
+    
+    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 void setMappedEntityB(EntityB1 bm) {
+        this.bm = bm;
+    }
+    
+    public EntityB1 getMappedEntityB() {
+        return bm;
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_MappedToOne.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_MappedToOne.java?rev=763427&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_MappedToOne.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityA_Embed_MappedToOne.java
Wed Apr  8 22:06:56 2009
@@ -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_MappedToOne implements Serializable {
+    @Id
+    Integer id;
+
+    @Column(length=30)
+    String name;
+    
+    @Basic(fetch=FetchType.LAZY)
+    int age;
+
+    @Embedded
+    protected Embed_MappedToOne 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_MappedToOne getEmbed() {
+        return embed;
+    }
+    
+    public void setEmbed(Embed_MappedToOne embed) {
+        this.embed = embed;
+    }
+}
+

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java?rev=763427&r1=763426&r2=763427&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/EntityB1.java
Wed Apr  8 22:06:56 2009
@@ -22,6 +22,7 @@
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
+import javax.persistence.OneToOne;
 
 @Entity
 public class EntityB1 implements Serializable {
@@ -31,6 +32,9 @@
 
     @Column(length=30)
     String name;
+    
+    @OneToOne
+    EntityA_Embed_MappedToOne entityA;
 
     public int getId() {
         return id;
@@ -47,5 +51,13 @@
     public void setName(String name) {
         this.name = name;
     }
+
+    public EntityA_Embed_MappedToOne getEntityA() {
+        return entityA;
+    }
+
+    public void setEntityA(EntityA_Embed_MappedToOne entityA) {
+        this.entityA = entityA;
+    }
 }
 

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java?rev=763427&r1=763426&r2=763427&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java
Wed Apr  8 22:06:56 2009
@@ -78,8 +78,8 @@
             Employee2.class, EmployeePK2.class, Department3.class,
             Employee3.class, EmployeeName3.class, Item1.class, Item2.class,
             Item3.class, Company1.class, Company2.class, Division.class, 
-            VicePresident.class,
-            DROP_TABLES);
+            VicePresident.class, EntityA_Embed_MappedToOne.class,
+            Embed_MappedToOne.class, DROP_TABLES);
     }
     
     public void testEntityA_Coll_String() {
@@ -94,6 +94,12 @@
         findEntityA_Embed_ToOne();
     }
 
+    public void testEntityA_Embed_MappedToOne() {
+        createEntityA_Embed_MappedToOne();
+        queryEntityA_Embed_MappedToOne();
+        findEntityA_Embed_MappedToOne();
+    }
+
     public void testEntityA_Coll_Embed_ToOne() {
         createEntityA_Coll_Embed_ToOne();
         queryEntityA_Coll_Embed_ToOne();
@@ -214,6 +220,44 @@
     }
 
     /*
+     * Create EntityA_Embed_MappedToOne
+     */
+    public void createEntityA_Embed_MappedToOne() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        createEntityA_Embed_MappedToOne(em, ID);
+        tran.begin();
+        em.flush();
+        tran.commit();
+        em.close();
+    }
+
+    public void createEntityA_Embed_MappedToOne(EntityManager em, int id) {
+        EntityA_Embed_MappedToOne a = new EntityA_Embed_MappedToOne();
+        a.setId(id);
+        a.setName("a" + id);
+        a.setAge(id);
+        Embed_MappedToOne embed = createEmbed_MappedToOne(em, id, a);
+        a.setEmbed(embed);
+        em.persist(a);
+    }
+
+    public Embed_MappedToOne createEmbed_MappedToOne(EntityManager em, int id, 
+        EntityA_Embed_MappedToOne a) {
+        Embed_MappedToOne embed = new Embed_MappedToOne();
+        embed.setName1("name1");
+        embed.setName2("name2");
+        embed.setName3("name3");
+        EntityB1 b = new EntityB1();
+        b.setId(id);
+        b.setName("bm" + id);
+        b.setEntityA(a);
+        embed.setMappedEntityB(b);
+        em.persist(b);
+        return embed;
+    }
+
+    /*
      * Create EntityA_Coll_Embed_ToOne
      */
     public void createEntityA_Coll_Embed_ToOne() {
@@ -671,6 +715,16 @@
     }
 
     /*
+     * Find EntityA_Embed_MappedToOne
+     */
+    public void findEntityA_Embed_MappedToOne() {
+        EntityManager em = emf.createEntityManager();
+        EntityA_Embed_MappedToOne a = em.find(EntityA_Embed_MappedToOne.class, ID);
+        checkEntityA_Embed_MappedToOne(a);
+        em.close();
+    }
+
+    /*
      * Find EntityA_Coll_Embed_ToOne
      */
     public void findEntityA_Coll_Embed_ToOne() {
@@ -770,6 +824,20 @@
     }
 
     /*
+     * check EntityA_Embed_MappedToOne
+     */
+    public void checkEntityA_Embed_MappedToOne(EntityA_Embed_MappedToOne a) {
+        int id = a.getId();
+        String name = a.getName();
+        int age = a.getAge();
+        assertEquals(1, id);
+        assertEquals("a" + id ,name);
+        assertEquals(1, age);
+        Embed_MappedToOne embed = a.getEmbed();
+        checkEmbed_MappedToOne(embed);
+    }
+
+    /*
      * check EntityA_Coll_Embed_ToOne
      */
     public void checkEntityA_Coll_Embed_ToOne(EntityA_Coll_Embed_ToOne a) {
@@ -796,6 +864,18 @@
         assertEquals("b" + b.getId(), b.getName());
     }
 
+    public void checkEmbed_MappedToOne(Embed_MappedToOne embed) {
+        String name1 = embed.getName1();
+        String name2 = embed.getName2();
+        String name3 = embed.getName3();
+        assertEquals("name1", name1);
+        assertEquals("name2", name2);
+        assertEquals("name3", name3);
+        EntityB1 b = embed.getMappedEntityB();
+        assertEquals(1, b.getId());
+        assertEquals("bm" + b.getId(), b.getName());
+    }
+
     /*
      * check EntityA_Embed_ToMany
      */
@@ -1013,6 +1093,38 @@
     }
 
     /*
+     * Query EntityA_Embed_MappedToOne
+     */
+    public void queryEntityA_Embed_MappedToOne() {
+        EntityManager em = emf.createEntityManager();
+        // test select embed object
+        String[] query = {
+            "select a.embed from " +
+                " EntityA_Embed_MappedToOne a ",
+            "select e from EntityA_Embed_MappedToOne a " +
+                " join a.embed e join e.bm bm where e.bm.id > 0 order by a.id",
+        };
+        for (int i = 0; i < query.length; i++) {
+            List<Object[]> rs = null;
+            rs = em.createQuery(query[i]).getResultList();
+            assertTrue(rs.size() > 0);
+            Object obj = rs.get(0);
+            assertTrue(obj instanceof Embed_MappedToOne);
+            assertTrue(((Embed_MappedToOne) obj).getMappedEntityB() != null);
+            em.clear();
+        }
+        EntityTransaction tran = em.getTransaction();
+        tran.begin();
+        Query q = em.createQuery("select a from EntityA_Embed_MappedToOne a");
+        List<EntityA_Embed_MappedToOne> as = q.getResultList();
+        for (EntityA_Embed_MappedToOne a : as) {
+            checkEntityA_Embed_MappedToOne(a);
+        }
+        tran.commit();
+        em.close();
+    }
+
+    /*
      * Query EntityA_Coll_Embed_ToOne
      */
     public void queryEntityA_Coll_Embed_ToOne() {



Mime
View raw message