cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r630476 - in /cayenne/main/trunk: framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/bridge/ framework/cayenne-jpa-unpublished/src/main/java/org/apa...
Date Sat, 23 Feb 2008 17:16:33 GMT
Author: aadamchik
Date: Sat Feb 23 09:16:32 2008
New Revision: 630476

URL: http://svn.apache.org/viewvc?rev=630476&view=rev
Log:
CAY-809 Support for explicit mapping of SQL results with SQLResultSetMapping
(JPA bridge)

Modified:
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaNativeQuery.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/ClassAnnotationProcessorFactory.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntity.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/resources/META-INF/cayenne/orm-coder.xml
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/MappingAssertion.java
    cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_6_SQLQueriesTest.java

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaNativeQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaNativeQuery.java?rev=630476&r1=630475&r2=630476&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaNativeQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaNativeQuery.java
Sat Feb 23 09:16:32 2008
@@ -47,9 +47,17 @@
     }
 
     public JpaNativeQuery(ObjectContext context, String sqlString, String dataMapName) {
+        this(context, sqlString, dataMapName, null);
+    }
+    
+    public JpaNativeQuery(ObjectContext context, String sqlString, String dataMapName, String
resultSetMappingName) {
         super(context);
         DataMap map = context.getEntityResolver().getDataMap(dataMapName);
         query = new SQLTemplate(map, processSQLString(sqlString));
+        
+        if(resultSetMappingName != null) {
+            query.setResultSetMapping(map.getResultSetMapping(resultSetMappingName));
+        }
     }
 
     protected String processSQLString(String sqlString) {

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java?rev=630476&r1=630475&r2=630476&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/ResourceLocalEntityManager.java
Sat Feb 23 09:16:32 2008
@@ -296,11 +296,7 @@
      * @return the new query instance
      */
     public Query createNativeQuery(String sqlString) {
-        checkClosed();
-
-        return new JpaNativeQuery(context, sqlString, factory
-                .getPersistenceUnitInfo()
-                .getPersistenceUnitName());
+        return createNativeQuery(sqlString, (String) null);
     }
 
     /**
@@ -313,8 +309,9 @@
     public Query createNativeQuery(String sqlString, String resultSetMapping) {
         checkClosed();
 
-        // TODO: Andrus, 2/10/2006 - implement
-        throw new UnsupportedOperationException("TODO");
+        return new JpaNativeQuery(context, sqlString, factory
+                .getPersistenceUnitInfo()
+                .getPersistenceUnitName(), resultSetMapping);
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java?rev=630476&r1=630475&r2=630476&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
Sat Feb 23 09:16:32 2008
@@ -35,6 +35,7 @@
 import org.apache.cayenne.jpa.map.JpaAttributes;
 import org.apache.cayenne.jpa.map.JpaBasic;
 import org.apache.cayenne.jpa.map.JpaColumn;
+import org.apache.cayenne.jpa.map.JpaColumnResult;
 import org.apache.cayenne.jpa.map.JpaDiscriminatorColumn;
 import org.apache.cayenne.jpa.map.JpaEmbeddable;
 import org.apache.cayenne.jpa.map.JpaEmbedded;
@@ -42,6 +43,8 @@
 import org.apache.cayenne.jpa.map.JpaEntityListener;
 import org.apache.cayenne.jpa.map.JpaEntityListeners;
 import org.apache.cayenne.jpa.map.JpaEntityMap;
+import org.apache.cayenne.jpa.map.JpaEntityResult;
+import org.apache.cayenne.jpa.map.JpaFieldResult;
 import org.apache.cayenne.jpa.map.JpaId;
 import org.apache.cayenne.jpa.map.JpaJoinColumn;
 import org.apache.cayenne.jpa.map.JpaManagedClass;
@@ -56,6 +59,7 @@
 import org.apache.cayenne.jpa.map.JpaQueryHint;
 import org.apache.cayenne.jpa.map.JpaRelationship;
 import org.apache.cayenne.jpa.map.JpaSecondaryTable;
+import org.apache.cayenne.jpa.map.JpaSqlResultSetMapping;
 import org.apache.cayenne.jpa.map.JpaTable;
 import org.apache.cayenne.jpa.map.JpaVersion;
 import org.apache.cayenne.map.DataMap;
@@ -72,6 +76,8 @@
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.project.ProjectPath;
+import org.apache.cayenne.query.EntityResult;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.util.BaseTreeVisitor;
 import org.apache.cayenne.util.HierarchicalTreeVisitor;
 import org.apache.cayenne.util.TraversalUtil;
@@ -163,6 +169,9 @@
         visitor.addChildVisitor(JpaEmbeddable.class, new JpaEmbeddableVisitor());
         visitor.addChildVisitor(JpaNamedQuery.class, new JpaNamedQueryVisitor());
         visitor.addChildVisitor(JpaPersistenceUnitMetadata.class, metadataVisitor);
+        visitor.addChildVisitor(
+                JpaSqlResultSetMapping.class,
+                new JpaSQLResultSetMappingVisitor());
         return visitor;
     }
 
@@ -637,6 +646,36 @@
         }
     }
 
+    class JpaSQLResultSetMappingVisitor extends BaseTreeVisitor {
+
+        @Override
+        public boolean onStartNode(ProjectPath path) {
+            JpaSqlResultSetMapping jpaMapping = (JpaSqlResultSetMapping) path.getObject();
+
+            SQLResultSetMapping mapping = new SQLResultSetMapping(jpaMapping.getName());
+
+            for (JpaColumnResult c : jpaMapping.getColumnResults()) {
+                mapping.addColumnResult(c.getName());
+            }
+
+            for (JpaEntityResult e : jpaMapping.getEntityResults()) {
+
+                EntityResult result = new EntityResult(e.getEntityClassName());
+                for (JpaFieldResult f : e.getFieldResults()) {
+                    result.addObjectField(f.getName(), f.getColumn());
+                }
+
+                // TODO: andrus 2/23/2008 - discriminator column...
+            }
+
+            // my understanding of JPA SQLResultSetMapping is that regardless of whether
+            // it belongs to entity or to the entire persistence unit, the scope is still
+            // persistence unit??
+            targetPath.firstInstanceOf(DataMap.class).addResultSetMapping(mapping);
+            return false;
+        }
+    }
+
     class JpaEntityVisitor extends NestedVisitor {
 
         JpaEntityVisitor() {
@@ -680,6 +719,9 @@
             addChildVisitor(JpaSecondaryTable.class, new JpaSecondaryTableVisitor());
             addChildVisitor(JpaNamedQuery.class, new JpaNamedQueryVisitor());
             addChildVisitor(JpaEntityListeners.class, listenersVisitor);
+            addChildVisitor(
+                    JpaSqlResultSetMapping.class,
+                    new JpaSQLResultSetMappingVisitor());
         }
 
         @Override

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/ClassAnnotationProcessorFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/ClassAnnotationProcessorFactory.java?rev=630476&r1=630475&r2=630476&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/ClassAnnotationProcessorFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/ClassAnnotationProcessorFactory.java
Sat Feb 23 09:16:32 2008
@@ -611,7 +611,7 @@
 
             SqlResultSetMapping annotation = element
                     .getAnnotation(SqlResultSetMapping.class);
-            entity.setSqlResultSetMapping(new JpaSqlResultSetMapping(annotation));
+            entity.getSqlResultSetMappings().add(new JpaSqlResultSetMapping(annotation));
         }
 
         @Override

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntity.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntity.java?rev=630476&r1=630475&r2=630476&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntity.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntity.java
Sat Feb 23 09:16:32 2008
@@ -42,7 +42,7 @@
     protected JpaDiscriminatorColumn discriminatorColumn;
     protected JpaSequenceGenerator sequenceGenerator;
     protected JpaTableGenerator tableGenerator;
-    protected JpaSqlResultSetMapping sqlResultSetMapping;
+    protected Collection<JpaSqlResultSetMapping> sqlResultSetMappings;
     protected Collection<JpaAttributeOverride> attributeOverrides;
     protected Collection<JpaAssociationOverride> associationOverrides;
     protected JpaEntity superEntity;
@@ -124,8 +124,8 @@
             encoder.print(namedNativeQueries);
         }
 
-        if (sqlResultSetMapping != null) {
-            sqlResultSetMapping.encodeAsXML(encoder);
+        if (sqlResultSetMappings != null) {
+            encoder.print(sqlResultSetMappings);
         }
 
         if (excludeDefaultListeners) {
@@ -292,13 +292,13 @@
         this.sequenceGenerator = sequenceGenerator;
     }
 
-    @TreeNodeChild
-    public JpaSqlResultSetMapping getSqlResultSetMapping() {
-        return sqlResultSetMapping;
-    }
-
-    public void setSqlResultSetMapping(JpaSqlResultSetMapping sqlResultSetMapping) {
-        this.sqlResultSetMapping = sqlResultSetMapping;
+    @TreeNodeChild(type = JpaSqlResultSetMapping.class)
+    public Collection<JpaSqlResultSetMapping> getSqlResultSetMappings() {
+        if(sqlResultSetMappings == null) {
+            sqlResultSetMappings = new ArrayList<JpaSqlResultSetMapping>();
+        }
+        
+        return sqlResultSetMappings;
     }
 
     @TreeNodeChild

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/resources/META-INF/cayenne/orm-coder.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/resources/META-INF/cayenne/orm-coder.xml?rev=630476&r1=630475&r2=630476&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/resources/META-INF/cayenne/orm-coder.xml
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/resources/META-INF/cayenne/orm-coder.xml
Sat Feb 23 09:16:32 2008
@@ -142,7 +142,7 @@
 		<property name="tableGenerator" xmlTag="table-generator"/>
 		<property name="namedQueries" xmlTag="named-query"/>
 		<property name="namedNativeQueries" xmlTag="named-native-query"/>
-		<property name="sqlResultSetMapping" xmlTag="sql-result-set-mapping"/>
+		<property name="sqlResultSetMappings" xmlTag="sql-result-set-mapping"/>
 		<property name="excludeDefaultListenersTrue" xmlTag="exclude-default-listeners"/>
 		<property name="excludeSuperclassListenersTrue" xmlTag="exclude-superclass-listeners"/>
 		<property name="entityListeners" xmlTag="entity-listeners"/>

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/MappingAssertion.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/MappingAssertion.java?rev=630476&r1=630475&r2=630476&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/MappingAssertion.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/test/java/org/apache/cayenne/jpa/conf/MappingAssertion.java
Sat Feb 23 09:16:32 2008
@@ -179,7 +179,7 @@
         assertNativeNamedQuery3(namedNativeQueryIt.next());
         assertNativeNamedQuery4(namedNativeQueryIt.next());
 
-        assertSqlResultSetMapping(entity1.getSqlResultSetMapping());
+        assertSqlResultSetMapping(entity1.getSqlResultSetMappings().iterator().next());
 
         assertTrue(entity1.isExcludeDefaultListeners());
         assertTrue(entity1.isExcludeSuperclassListeners());

Modified: cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_6_SQLQueriesTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_6_SQLQueriesTest.java?rev=630476&r1=630475&r2=630476&view=diff
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_6_SQLQueriesTest.java
(original)
+++ cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_6_SQLQueriesTest.java
Sat Feb 23 09:16:32 2008
@@ -54,15 +54,14 @@
                 1,
                 "X").insert(2, "Y");
 
-        // TODO: andrus 2/18/2008 map named JPA SQLResultSetMappings to Cayenne
-        // EntityManager em = getEntityManager();
-        // Query query = em.createNativeQuery(
-        // "SELECT ID as X, ID + 5 as Y, ID + 6 as Z FROM SimpleEntity ORDER BY ID",
-        // "rs1");
-        //
-        // List result = query.getResultList();
-        // assertNotNull(result);
-        // assertEquals(2, result.size());
-        // assertTrue(result.get(0) instanceof Object[]);
+        EntityManager em = getEntityManager();
+        Query query = em.createNativeQuery(
+                "SELECT ID as X, ID + 5 as Y, ID + 6 as Z FROM SimpleEntity ORDER BY ID",
+                "rs1");
+
+        List result = query.getResultList();
+        assertNotNull(result);
+        assertEquals(2, result.size());
+        assertTrue(result.get(0) instanceof Object[]);
     }
 }



Mime
View raw message