openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ss...@apache.org
Subject svn commit: r498850 - in /incubator/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/query/ openjpa-persi...
Date Mon, 22 Jan 2007 23:33:10 GMT
Author: ssegu
Date: Mon Jan 22 15:33:08 2007
New Revision: 498850

URL: http://svn.apache.org/viewvc?view=rev&rev=498850
Log:
Fixes to parse and use SqlResultSetMapping, SqlResultSetMappings annotations.

Added:
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java
    incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestResultSetMapping.java
Modified:
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
    incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/QueryResultMapping.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractMetaDataFactory.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/DelegatingMetaDataFactory.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataFactory.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/NoneMetaDataFactory.java
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/QueryMetaData.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
    incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceMetaDataFactory.java

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/MappingRepository.java
Mon Jan 22 15:33:08 2007
@@ -199,8 +199,11 @@
         if ((getSourceMode() & MODE_QUERY) == 0)
             return null;
 
+        if (cls == null)
+            cls = getMetaDataFactory()
+                    .getResultSetMappingScope(name, envLoader);
         // not in cache; load
-        getMetaDataFactory().load(cls, MODE_QUERY, envLoader);
+        getMetaDataFactory().load(cls, MODE_META | MODE_MAPPING, envLoader);
         return (QueryResultMapping) _results.get(key);
     }
 

Modified: incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/QueryResultMapping.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/QueryResultMapping.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/QueryResultMapping.java
(original)
+++ incubator/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/QueryResultMapping.java
Mon Jan 22 15:33:08 2007
@@ -409,9 +409,12 @@
                 if (last == null)
                     throw new MetaDataException(_loc.get("untraversable-path",
                         QueryResultMapping.this, _candidate, path));
-                assertSingleColumn(last.getColumns(), path);
-                Column col = last.getColumns()[0];
-
+                Column[] cols = last.getColumns();
+                if (last.isVersion())
+                    cols = candidate.getVersion().getColumns();
+                assertSingleColumn(cols, path);
+                Column col = cols[0];
+                
                 // special-case oid fields, since path lists supplied for
                 // them at runtime don't include the embedded fields
                 if (fm != null && fm.getDeclaredTypeCode() == JavaTypes.OID) {

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractMetaDataFactory.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractMetaDataFactory.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractMetaDataFactory.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/AbstractMetaDataFactory.java
Mon Jan 22 15:33:08 2007
@@ -97,6 +97,11 @@
         return null;
     }
 
+    public Class getResultSetMappingScope(String resultSetMappingName,
+        ClassLoader loader) {
+        return null;
+    }
+
     public ClassArgParser newClassArgParser() {
         return new ClassArgParser();
     }

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/DelegatingMetaDataFactory.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/DelegatingMetaDataFactory.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/DelegatingMetaDataFactory.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/DelegatingMetaDataFactory.java
Mon Jan 22 15:33:08 2007
@@ -103,6 +103,11 @@
         return _delegate.getQueryScope(queryName, loader);
     }
 
+    public Class getResultSetMappingScope(String resultSetMappingName,
+        ClassLoader loader) {
+        return _delegate.getResultSetMappingScope(resultSetMappingName, loader);
+    }
+
     public void clear() {
         _delegate.clear();
     }
@@ -113,5 +118,5 @@
 
     public void addFieldExtensionKeys(Collection exts) {
         _delegate.addFieldExtensionKeys(exts);
-	}
+    }
 }

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataFactory.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataFactory.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataFactory.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataFactory.java
Mon Jan 22 15:33:08 2007
@@ -116,6 +116,12 @@
     public Class getQueryScope(String queryName, ClassLoader loader);
 
     /**
+     * Return the type defining the given result set mapping name, if any.
+     */
+    public Class getResultSetMappingScope(String resultSetMappingName,
+        ClassLoader loader);
+    
+    /**
      * Return a properly-configured class arg parser for our expected
      * metadata format.
      */

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/NoneMetaDataFactory.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/NoneMetaDataFactory.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/NoneMetaDataFactory.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/NoneMetaDataFactory.java
Mon Jan 22 15:33:08 2007
@@ -76,6 +76,11 @@
         return null;
     }
 
+    public Class getResultSetMappingScope(String resultSetMappingName,
+        ClassLoader loader) {
+        return null;
+    }
+
     public ClassArgParser newClassArgParser() {
         return new ClassArgParser();
     }

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/QueryMetaData.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/QueryMetaData.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/QueryMetaData.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/QueryMetaData.java
Mon Jan 22 15:33:08 2007
@@ -52,7 +52,7 @@
     private String[] _comments;
     private List _hintKeys;
     private List _hintVals;
-
+    private String _resultSetMappingName;
     /**
      * Construct with the given name.
      */
@@ -178,6 +178,14 @@
         _hintVals.add(value);
     }
 
+    public String getResultSetMappingName() {
+        return _resultSetMappingName;
+    }
+
+    public void setResultSetMappingName(String setMappingName) {
+        _resultSetMappingName = setMappingName;
+    }
+    
     /**
      * Set query template information into the given concrete
      * query instance. However, the language, query string, and
@@ -193,6 +201,8 @@
             query.setResultType(_res);
         if (_readOnly != null)
             query.setReadOnly(_readOnly.booleanValue());
+        if (_resultSetMappingName != null)
+            query.setResultMapping(null, _resultSetMappingName);
     }
 
     /**

Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java?view=auto&rev=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java
(added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/SimpleEntity.java
Mon Jan 22 15:33:08 2007
@@ -0,0 +1,90 @@
+/*
+ * 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.query;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EntityResult;
+import javax.persistence.FieldResult;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.NamedNativeQueries;
+import javax.persistence.NamedNativeQuery;
+import javax.persistence.SqlResultSetMapping;
+import javax.persistence.Table;
+
+@NamedNativeQueries( { 
+    @NamedNativeQuery(name = "findSimpleEntitites",
+        query = "SELECT ID, NAME, VALUE FROM SIMPLE_ENTITY", 
+        resultSetMapping = "simpleEntitiesResult") })
+
+@SqlResultSetMapping(name = "simpleEntitiesResult",
+    entities = @EntityResult(
+    entityClass = org.apache.openjpa.persistence.query.SimpleEntity.class, 
+    fields = {@FieldResult(name = "id", column = "ID"),
+        @FieldResult(name = "name", column = "NAME"),
+        @FieldResult(name = "value", column = "VALUE") }))
+@Entity(name = "simple")
+@Table(name = "SIMPLE_ENTITY")
+public class SimpleEntity {
+
+    @Id
+    @GeneratedValue
+    @Column(name = "ID")
+    private long id;
+
+    @Basic
+    @Column(name = "NAME")
+    private String name;
+
+    @Basic
+    @Column(name = "VALUE")
+    private String value;
+
+    public SimpleEntity() {
+    }
+
+    public SimpleEntity(String name, String value) {
+        this();
+        this.name = name;
+        this.value = value;
+    }
+
+    public long getId() {
+        return this.id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+}
\ No newline at end of file

Added: incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestResultSetMapping.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestResultSetMapping.java?view=auto&rev=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestResultSetMapping.java
(added)
+++ incubator/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestResultSetMapping.java
Mon Jan 22 15:33:08 2007
@@ -0,0 +1,77 @@
+/*
+ * 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.query;
+
+import java.util.HashMap;
+import java.util.Iterator;
+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;
+
+public class TestResultSetMapping extends TestCase {
+
+    private EntityManagerFactory emf;
+
+    public void setUp() {
+        Map props = new HashMap();
+        props.put("openjpa.MetaDataFactory", "jpa(Types=" + 
+            org.apache.openjpa.persistence.query.SimpleEntity.class.getName() + ")");
+        emf = Persistence.createEntityManagerFactory("test", props);
+
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(new SimpleEntity("tName", "tValue"));
+        em.getTransaction().commit();
+        em.close();
+    }
+
+    public void tearDown() {
+        if (emf == null)
+            return;
+        try {
+            EntityManager em = emf.createEntityManager();
+            em.getTransaction().begin();
+            em.createQuery("delete from simple").executeUpdate();
+            em.getTransaction().commit();
+            em.close();
+            emf.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public void testSimpleQuery() {
+        EntityManager em = emf.createEntityManager();
+        Query q = em.createNamedQuery("findSimpleEntitites");
+        List res = q.getResultList();
+        assertNotNull(res);
+        for (Iterator resultIter = res.iterator(); resultIter.hasNext();) {
+            assertSame(resultIter.next().getClass(), SimpleEntity.class);
+        }
+        em.close();
+    }
+
+    public static void main(String[] args) {
+        TestRunner.run(TestResultSetMapping.class);
+    }
+}
\ No newline at end of file

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
(original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
Mon Jan 22 15:33:08 2007
@@ -1555,6 +1555,9 @@
             else if (!void.class.equals(res))
                 meta.setResultType(res);
 
+            if (!StringUtils.isEmpty(query.resultSetMapping()))
+                meta.setResultSetMappingName(query.resultSetMapping());
+
             meta.setSource(getSourceFile(), (el instanceof Class) ? el : null,
                 meta.SRC_ANNOTATIONS);
             if (isMetaDataMode())

Modified: incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceMetaDataFactory.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceMetaDataFactory.java?view=diff&rev=498850&r1=498849&r2=498850
==============================================================================
--- incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceMetaDataFactory.java
(original)
+++ incubator/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceMetaDataFactory.java
Mon Jan 22 15:33:08 2007
@@ -32,6 +32,8 @@
 import javax.persistence.NamedQuery;
 import javax.persistence.NamedNativeQueries;
 import javax.persistence.NamedNativeQuery;
+import javax.persistence.SqlResultSetMapping;
+import javax.persistence.SqlResultSetMappings;
 
 import org.apache.openjpa.lib.conf.Configurable;
 import org.apache.openjpa.lib.conf.Configuration;
@@ -294,9 +296,40 @@
         return null;
     }
 
+    @Override
+    public Class getResultSetMappingScope(String rsMappingName,
+        ClassLoader loader) {
+        if (rsMappingName == null)
+            return null;
+        
+        Collection classes = repos.loadPersistentTypes(false, loader);
+        for (Class cls : (Collection<Class>) classes) {
+            
+            if (cls.isAnnotationPresent(SqlResultSetMapping.class) && 
+                hasRSMapping(rsMappingName, (SqlResultSetMapping) cls.
+                getAnnotation(SqlResultSetMapping.class)))
+                return cls;
+            
+            if (cls.isAnnotationPresent(SqlResultSetMappings.class) && 
+                hasRSMapping(rsMappingName, ((SqlResultSetMappings) cls.
+                getAnnotation(SqlResultSetMappings.class)).value()))
+                return cls;
+        }
+        return null;
+    }
+
     private boolean hasNamedQuery(String query, NamedQuery... queries) {
         for (NamedQuery q : queries) {
             if (query.equals(q.name()))
+                return true;
+        }
+        return false;
+    }
+
+    private boolean hasRSMapping(String rsMapping,
+        SqlResultSetMapping... mappings) {
+        for (SqlResultSetMapping m : mappings) {
+            if (rsMapping.equals(m.name()))
                 return true;
         }
         return false;



Mime
View raw message