openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fayw...@apache.org
Subject svn commit: r765800 - in /openjpa/trunk: openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/ openjpa-kernel/src/main/java/org/apache/openjpa/meta/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/ openjpa-p...
Date Thu, 16 Apr 2009 23:24:08 GMT
Author: faywang
Date: Thu Apr 16 23:24:07 2009
New Revision: 765800

URL: http://svn.apache.org/viewvc?rev=765800&view=rev
Log:
OPENJPA-1041: allow orderBy on nested embeddables

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Person.java
  (with props)
Modified:
    openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/JDBCRelatedFieldOrder.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java

Modified: openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/JDBCRelatedFieldOrder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/JDBCRelatedFieldOrder.java?rev=765800&r1=765799&r2=765800&view=diff
==============================================================================
--- openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/JDBCRelatedFieldOrder.java
(original)
+++ openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/JDBCRelatedFieldOrder.java
Thu Apr 16 23:24:07 2009
@@ -66,7 +66,9 @@
     }
 
     public void order(Select sel, ClassMapping elem, Joins joins) {
-        FieldMapping fm = elem.getFieldMapping(_fm.getIndex());
+        FieldMapping fm = _fm;
+        if (elem != null)
+            fm = elem.getFieldMapping(_fm.getIndex());
         sel.orderBy(fm.getColumns(), _asc, joins, false);
     }
 }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java?rev=765800&r1=765799&r2=765800&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
Thu Apr 16 23:24:07 2009
@@ -874,13 +874,32 @@
         if (meta == null)
             throw new MetaDataException(_loc.get("nonpc-field-orderable",
                 owner, name));
-        FieldMetaData rel = meta.getField(name);
+        FieldMetaData rel = getOrderByField(meta, name);
         if (rel == null)
             throw new MetaDataException(_loc.get("bad-field-orderable",
                 owner, name));
         return newRelatedFieldOrder(owner, rel, asc);
     }
 
+    
+    public FieldMetaData getOrderByField(ClassMetaData meta, String orderBy) {
+        FieldMetaData field = meta.getField(orderBy);
+        if (field != null)
+            return field;
+        int dotIdx = orderBy.indexOf("."); 
+        if ( dotIdx == -1)
+            return null;
+        String fieldName = orderBy.substring(0, dotIdx);
+        FieldMetaData field1 = meta.getField(fieldName);
+        if (field1 == null)
+            return null;
+        ClassMetaData meta1 = field1.getEmbeddedMetaData();
+        if (meta1 == null)
+            return null;
+        String mappedBy1 = orderBy.substring(dotIdx + 1);
+        return getOrderByField(meta1, mappedBy1);
+    }  
+    
     /**
      * Order by the field value.
      */

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java?rev=765800&r1=765799&r2=765800&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Address.java
Thu Apr 16 23:24:07 2009
@@ -21,7 +21,7 @@
 import javax.persistence.*;
 
 @Entity
-@Table(name="ADDR_ATTROVER")
+@Table(name="ADR_AO")
 public class Address {
     @Id int id;
     @Column(length = 20)

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Person.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Person.java?rev=765800&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Person.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Person.java
Thu Apr 16 23:24:07 2009
@@ -0,0 +1,52 @@
+package org.apache.openjpa.persistence.embed.attrOverrides;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.ElementCollection;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.OrderBy;
+import javax.persistence.Table;
+
+@Entity 
+@Table(name="PSN")
+public class Person {
+    @Id 
+    protected String ssn;
+    protected String name;
+
+    @ElementCollection
+    @OrderBy("zipcode.zip, zipcode.plusFour")
+    protected List<Address> residences = new ArrayList<Address>();
+    
+    public String getSsn() {
+        return ssn;
+    }
+    
+    public void setSsn(String ssn) {
+        this.ssn = ssn;
+    }
+    
+    public String getName() {
+        return name;
+    }
+    
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public List<Address> getResidences() {
+        return residences;
+    }
+    
+    public void setResidences(List<Address> residences) {
+        this.residences = residences;
+    }
+    
+    public void addResidence(Address residence) {
+        residences.add(residence);
+    }
+}
+
+

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Person.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java?rev=765800&r1=765799&r2=765800&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/TestAttrOverrides.java
Thu Apr 16 23:24:07 2009
@@ -36,7 +36,7 @@
     public void setUp() throws Exception {
         super.setUp(DROP_TABLES, Address.class, Customer.class, 
             PropertyInfo.class, PropertyOwner.class, PropertyRecord.class,
-            Zipcode.class);
+            Zipcode.class, Person.class);
     }
 
     /**
@@ -63,6 +63,16 @@
         assertAttrOverrides("PROPREC_ATTROVER_parcels");
     }
     
+    /**
+     * This is spec 10.1.35. Example 3
+     * Test OrderBy on embeddable field
+     */
+    public void testEmbeddableOrderBy() {
+        sql.clear();
+        createObj3();
+        findObj3();
+    }
+
     public void createObj1() {
         EntityManager em = emf.createEntityManager();
         EntityTransaction tran = em.getTransaction();
@@ -212,4 +222,54 @@
         if (!found)
             fail();
     }
+    
+    public void createObj3() {
+        EntityManager em = emf.createEntityManager();
+        EntityTransaction tran = em.getTransaction();
+        for (int i = 0; i < numPersons; i++)
+            createPerson(em, eId++);
+        tran.begin();
+        em.flush();
+        tran.commit();
+        em.close();
+    }
+
+    public Person createPerson(EntityManager em, int id) {
+        Person p = new Person();
+        p.setSsn("ssn" + id);
+        p.setName("name_" + id);
+    
+        for (int i = 4; i > 0; i--) {
+            Address addr = new Address();
+            addr.setCity("city_" + id + "_" + i );
+            addr.setState("state_" + id + "_"  + i);
+            addr.setStreet("street_" + id + "_"  + i);
+            Zipcode zipCode = new Zipcode();
+            zipCode.setZip("zip_" + id + "_" + i);
+            zipCode.setPlusFour("plusFour_" + id + "_" + i);
+            addr.setZipcode(zipCode);
+            p.addResidence(addr);
+        }
+        em.persist(p);
+        return p;
+    }
+
+    public void findObj3() {
+        EntityManager em = emf.createEntityManager();
+        Person p = em.find(Person.class, "ssn1");
+        List<Address> residences = p.getResidences();
+        assertEquals(4, residences.size());
+        int i = 1;
+        for (Address a : residences) {
+            String zip = a.getZipcode().getZip();
+            String plusFour = a.getZipcode().getPlusFour();
+            String expZip = "zip_1_";
+            String expPlusFour = "plusFour_1_";
+            expZip = expZip + i;
+            expPlusFour = expPlusFour + i;
+            assertEquals(expZip, zip);
+            assertEquals(expPlusFour, plusFour);
+            i++;
+        }
+   }
 }
\ No newline at end of file

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java?rev=765800&r1=765799&r2=765800&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/attrOverrides/Zipcode.java
Thu Apr 16 23:24:07 2009
@@ -23,9 +23,9 @@
 
 @Embeddable
 public class Zipcode {
-    @Column(length = 5)
+    @Column(length = 20)
 	protected String zip;
-    @Column(length = 4)
+    @Column(length = 20)
 	protected String plusFour;
 	
 	public String getZip() {

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java?rev=765800&r1=765799&r2=765800&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
Thu Apr 16 23:24:07 2009
@@ -1574,10 +1574,12 @@
             fmd.getElement().setDeclaredType(anno.targetClass());
         fmd.setInDefaultFetchGroup(anno.fetch() == FetchType.EAGER);
         fmd.setElementCollection(true);
-        if (JavaTypes.maybePC(fmd.getElement())) {
-            fmd.getElement().setEmbedded(true);
-            if (fmd.getElement().getEmbeddedMetaData() == null)
-                fmd.getElement().addEmbeddedMetaData();
+        ValueMetaData elem = fmd.getElement();
+        boolean isEnum = elem.getDeclaredType().isEnum();
+        if (!isEnum && JavaTypes.maybePC(elem)) {
+            elem.setEmbedded(true);
+            if (elem.getEmbeddedMetaData() == null)
+                elem.addEmbeddedMetaData();
         }
     }
     



Mime
View raw message