cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r605986 - 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/conf/ framework/cayenne-jpa-unpublished/src/main/java/org/apach...
Date Thu, 20 Dec 2007 18:06:57 GMT
Author: aadamchik
Date: Thu Dec 20 10:06:56 2007
New Revision: 605986

URL: http://svn.apache.org/viewvc?rev=605986&view=rev
Log:
CAY-946 JPA: property-based access to persistent objects
(sort of works, but requires more testing... and of course a working enhancer)

Modified:
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/EntityMapAnnotationLoader.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/enhancer/JpaAccessorVisitor.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntityMap.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java
    cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_1_PeristentFieldsAndPropertiesTest.java

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java?rev=605986&r1=605985&r2=605986&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/Provider.java
Thu Dec 20 10:06:56 2007
@@ -220,17 +220,6 @@
 
             boolean isJTA = isJta(unit, map);
 
-            // configure Cayenne domain
-            domain = new DataDomain(name);
-            ClassDescriptorMap descriptors = domain
-                    .getEntityResolver()
-                    .getClassDescriptorMap();
-            FaultFactory faultFactory = new SingletonFaultFactory();
-            descriptors.addFactory(new JpaClassDescriptorFactory(
-                    descriptors,
-                    faultFactory));
-            configuration.addDomain(domain);
-
             EntityMapLoader loader = new EntityMapLoader(unit);
 
             // add transformer before DataMapConverter starts loading the classes via app
@@ -246,6 +235,18 @@
 
             DataMapConverter converter = new DataMapConverter();
             DataMap cayenneMap = converter.toDataMap(name, loader.getContext());
+
+            // configure Cayenne domain
+            domain = new DataDomain(name);
+            ClassDescriptorMap descriptors = domain
+                    .getEntityResolver()
+                    .getClassDescriptorMap();
+            FaultFactory faultFactory = new SingletonFaultFactory();
+            descriptors.addFactory(new JpaClassDescriptorFactory(
+                    loader.getEntityMap(),
+                    descriptors,
+                    faultFactory));
+            configuration.addDomain(domain);
 
             // TODO: andrus, 2/3/2007 - clarify this logic.... JTA EM may not always mean
             // JTA DS?

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/EntityMapAnnotationLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/EntityMapAnnotationLoader.java?rev=605986&r1=605985&r2=605986&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/EntityMapAnnotationLoader.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/conf/EntityMapAnnotationLoader.java
Thu Dec 20 10:06:56 2007
@@ -174,7 +174,7 @@
     public void loadClassMapping(Class<?> managedClass) throws JpaProviderException
{
 
         // avoid duplicates loaded from annotations per CAY-756
-        if (context.getEntityMap().containsManagedClass(managedClass.getName())) {
+        if (context.getEntityMap().getManagedClass(managedClass.getName()) != null) {
             context.recordConflict(new SimpleValidationFailure(
                     managedClass.getName(),
                     "Duplicate managed class declaration " + managedClass.getName()));

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/enhancer/JpaAccessorVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/enhancer/JpaAccessorVisitor.java?rev=605986&r1=605985&r2=605986&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/enhancer/JpaAccessorVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/enhancer/JpaAccessorVisitor.java
Thu Dec 20 10:06:56 2007
@@ -19,6 +19,7 @@
 package org.apache.cayenne.jpa.enhancer;
 
 import org.apache.cayenne.enhancer.AccessorVisitor;
+import org.apache.cayenne.jpa.map.AccessType;
 import org.apache.cayenne.jpa.map.JpaClassDescriptor;
 import org.apache.cayenne.jpa.map.JpaPropertyDescriptor;
 import org.objectweb.asm.ClassVisitor;
@@ -37,7 +38,8 @@
 
     @Override
     protected boolean isEnhancedProperty(String property) {
-        return descriptor.getProperty(property) != null;
+        return descriptor.getAccess() != AccessType.PROPERTY
+                && descriptor.getProperty(property) != null;
     }
 
     @Override

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntityMap.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntityMap.java?rev=605986&r1=605985&r2=605986&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntityMap.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/map/JpaEntityMap.java
Thu Dec 20 10:06:56 2007
@@ -109,12 +109,12 @@
         if (mappedSuperclasses != null) {
             encoder.print(mappedSuperclasses);
         }
-        
-        if(entities != null) {
+
+        if (entities != null) {
             encoder.print(entities);
         }
-        
-        if(embeddables != null) {
+
+        if (embeddables != null) {
             encoder.print(embeddables);
         }
 
@@ -123,9 +123,9 @@
     }
 
     /**
-     * Returns true if a given managed class is already loaded.
+     * Returns an existing managed class, or null if no match is found.
      */
-    public boolean containsManagedClass(String className) {
+    public JpaManagedClass getManagedClass(String className) {
         if (className == null) {
             throw new IllegalArgumentException("Null class name");
         }
@@ -133,7 +133,7 @@
         if (mappedSuperclasses != null) {
             for (JpaMappedSuperclass object : mappedSuperclasses) {
                 if (className.equals(object.getClassName())) {
-                    return true;
+                    return object;
                 }
             }
         }
@@ -141,7 +141,7 @@
         if (entities != null) {
             for (JpaEntity object : entities) {
                 if (className.equals(object.getClassName())) {
-                    return true;
+                    return object;
                 }
             }
         }
@@ -149,12 +149,12 @@
         if (embeddables != null) {
             for (JpaEmbeddable object : embeddables) {
                 if (className.equals(object.getClassName())) {
-                    return true;
+                    return object;
                 }
             }
         }
 
-        return false;
+        return null;
     }
 
     /**
@@ -205,8 +205,8 @@
         if (entityClassName == null) {
             throw new IllegalArgumentException("Null entity class name");
         }
-        
-        if(entities == null) {
+
+        if (entities == null) {
             return null;
         }
 
@@ -218,7 +218,7 @@
 
         return null;
     }
-    
+
     /**
      * Returns a JpaEmbeddable describing a given embeddable class.
      */
@@ -230,7 +230,7 @@
 
         return embeddableForClass(embeddableClass.getName());
     }
-    
+
     /**
      * Returns a JpaEmbeddable describing a given embeddable class.
      */
@@ -238,8 +238,8 @@
         if (embeddableClassName == null) {
             throw new IllegalArgumentException("Null embeddable class name");
         }
-        
-        if(embeddables == null) {
+
+        if (embeddables == null) {
             return null;
         }
 
@@ -251,7 +251,6 @@
 
         return null;
     }
-
 
     public AccessType getAccess() {
         return access;

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java?rev=605986&r1=605985&r2=605986&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/JpaClassDescriptorFactory.java
Thu Dec 20 10:06:56 2007
@@ -24,20 +24,51 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.cayenne.jpa.map.AccessType;
+import org.apache.cayenne.jpa.map.JpaEntityMap;
+import org.apache.cayenne.jpa.map.JpaManagedClass;
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.reflect.Accessor;
+import org.apache.cayenne.reflect.BeanAccessor;
 import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.reflect.ClassDescriptorMap;
 import org.apache.cayenne.reflect.FaultFactory;
 import org.apache.cayenne.reflect.PersistentDescriptor;
 import org.apache.cayenne.reflect.Property;
+import org.apache.cayenne.reflect.PropertyException;
 import org.apache.cayenne.reflect.pojo.EnhancedPojoDescriptorFactory;
 
 public class JpaClassDescriptorFactory extends EnhancedPojoDescriptorFactory {
 
-    public JpaClassDescriptorFactory(ClassDescriptorMap descriptorMap,
-            FaultFactory faultFactory) {
+    protected JpaEntityMap entityMap;
+
+    public JpaClassDescriptorFactory(JpaEntityMap entityMap,
+            ClassDescriptorMap descriptorMap, FaultFactory faultFactory) {
         super(descriptorMap, faultFactory);
+        this.entityMap = entityMap;
+    }
+
+    @Override
+    protected Accessor createAccessor(
+            PersistentDescriptor descriptor,
+            String propertyName,
+            Class<?> propertyType) throws PropertyException {
+
+        String className = descriptor.getObjectClass().getName();
+        JpaManagedClass managedClass = entityMap.getManagedClass(className);
+        if (managedClass == null) {
+            throw new IllegalArgumentException("Not a managed class: " + className);
+        }
+
+        if (managedClass.getAccess() == AccessType.PROPERTY) {
+            return new BeanAccessor(
+                    descriptor.getObjectClass(),
+                    propertyName,
+                    propertyType);
+        }
+        else {
+            return super.createAccessor(descriptor, propertyName, propertyType);
+        }
     }
 
     protected void createToManyListProperty(

Modified: cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_1_PeristentFieldsAndPropertiesTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_1_PeristentFieldsAndPropertiesTest.java?rev=605986&r1=605985&r2=605986&view=diff
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_1_PeristentFieldsAndPropertiesTest.java
(original)
+++ cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_1_PeristentFieldsAndPropertiesTest.java
Thu Dec 20 10:06:56 2007
@@ -46,13 +46,18 @@
                 "property1"));
     }
 
-    // TODO: andrus 8/10/2006 - fails
-    public void _testPropertyBasedPersistence() {
+    public void testPropertyBasedPersistence() throws Exception {
+        getDbHelper().deleteAll("PropertyPersistenceEntity");
+
         PropertyPersistenceEntity o1 = new PropertyPersistenceEntity();
         o1.setProperty1("p1");
         o1.setProperty2(true);
         getEntityManager().persist(o1);
         getEntityManager().getTransaction().commit();
+
+        assertEquals("p1", getDbHelper().getObject(
+                "PropertyPersistenceEntity",
+                "property1"));
     }
 
     public void testSkipTransientProperties() {



Mime
View raw message