You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2007/12/24 18:17:30 UTC

svn commit: r606712 - in /cayenne/main/trunk: framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/ framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/reflect/ itests/jpa-chapter2/src/main/java/org/apache/ca...

Author: aadamchik
Date: Mon Dec 24 09:17:27 2007
New Revision: 606712

URL: http://svn.apache.org/viewvc?rev=606712&view=rev
Log:
CAY-946 JPA: property-based access to persistent objects
(generating correct Embeddable descriptors for property-based access)

Added:
    cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddable.java
    cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddedEntity.java
    cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddable1.java
    cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddedEntity.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.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/main/resources/META-INF/persistence.xml
    cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_5_EmbeddableTest.java
    cayenne/main/trunk/itests/jpa-chapter2/src/test/resources/schema-hsqldb.sql

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java?rev=606712&r1=606711&r2=606712&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/reflect/PersistentDescriptorFactory.java Mon Dec 24 09:17:27 2007
@@ -75,7 +75,8 @@
                 for (ObjAttribute objAttribute : embedded.getAttributes()) {
                     createEmbeddedAttributeProperty(descriptor, embedded, objAttribute);
                 }
-            } else if (attribute instanceof ObjAttribute) {
+            }
+            else if (attribute instanceof ObjAttribute) {
                 createAttributeProperty(descriptor, (ObjAttribute) attribute);
             }
         }
@@ -151,15 +152,15 @@
 
         String embeddableName = propertyPath.substring(lastDot + 1);
 
+        EmbeddableDescriptor embeddableDescriptor = createEmbeddableDescriptor(embeddedAttribute);
+
         Accessor embeddedAccessor = createAccessor(descriptor, embeddedAttribute
                 .getName(), embeddableClass);
         Accessor embeddedableAccessor = createEmbeddableAccessor(
-                embeddableClass,
+                embeddableDescriptor,
                 embeddableName,
                 attribute.getJavaClass());
 
-        EmbeddableDescriptor embeddableDescriptor = createEmbeddableDescriptor(embeddedAttribute);
-
         Accessor accessor = new EmbeddedFieldAccessor(
                 embeddableDescriptor,
                 embeddedAccessor,
@@ -235,12 +236,15 @@
      * Creates an accessor for the property of the embeddable class.
      */
     protected Accessor createEmbeddableAccessor(
-            Class<?> embeddableClass,
+            EmbeddableDescriptor descriptor,
             String propertyName,
             Class<?> propertyType) {
-        return new FieldAccessor(embeddableClass, propertyName, propertyType);
+        return new FieldAccessor(descriptor.getObjectClass(), propertyName, propertyType);
     }
 
+    /**
+     * Creates a descriptor of the embedded property.
+     */
     protected EmbeddableDescriptor createEmbeddableDescriptor(
             EmbeddedAttribute embeddedAttribute) {
         // TODO: andrus, 11/19/2007 = avoid creation of descriptor for every property of

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=606712&r1=606711&r2=606712&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 Mon Dec 24 09:17:27 2007
@@ -32,6 +32,7 @@
 import org.apache.cayenne.reflect.BeanAccessor;
 import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.reflect.ClassDescriptorMap;
+import org.apache.cayenne.reflect.EmbeddableDescriptor;
 import org.apache.cayenne.reflect.FaultFactory;
 import org.apache.cayenne.reflect.PersistentDescriptor;
 import org.apache.cayenne.reflect.Property;
@@ -46,6 +47,29 @@
             ClassDescriptorMap descriptorMap, FaultFactory faultFactory) {
         super(descriptorMap, faultFactory);
         this.entityMap = entityMap;
+    }
+    
+    @Override
+    protected Accessor createEmbeddableAccessor(
+            EmbeddableDescriptor descriptor,
+            String propertyName,
+            Class<?> propertyType) {
+        
+        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.createEmbeddableAccessor(descriptor, propertyName, propertyType);
+        }
     }
 
     @Override

Added: cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddable.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddable.java?rev=606712&view=auto
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddable.java (added)
+++ cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddable.java Mon Dec 24 09:17:27 2007
@@ -0,0 +1,37 @@
+/*****************************************************************
+ *   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.cayenne.jpa.itest.ch2.entity;
+
+import javax.persistence.Basic;
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class PropertyEmbeddable {
+
+    protected String xproperty1;
+
+    @Basic
+    public String getProperty1() {
+        return xproperty1;
+    }
+
+    public void setProperty1(String property1) {
+        this.xproperty1 = property1;
+    }
+}

Added: cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddedEntity.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddedEntity.java?rev=606712&view=auto
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddedEntity.java (added)
+++ cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/PropertyEmbeddedEntity.java Mon Dec 24 09:17:27 2007
@@ -0,0 +1,48 @@
+/*****************************************************************
+ *   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.cayenne.jpa.itest.ch2.entity;
+
+import javax.persistence.Embedded;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class PropertyEmbeddedEntity {
+
+    protected int xid;
+    protected PropertyEmbeddable xembeddable;
+
+    @Embedded
+    public PropertyEmbeddable getEmbeddable() {
+        return xembeddable;
+    }
+
+    public void setEmbeddable(PropertyEmbeddable embeddable) {
+        this.xembeddable = embeddable;
+    }
+
+    @Id
+    public int getId() {
+        return xid;
+    }
+
+    public void setId(int id) {
+        this.xid = id;
+    }
+}

Added: cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddable1.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddable1.java?rev=606712&view=auto
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddable1.java (added)
+++ cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddable1.java Mon Dec 24 09:17:27 2007
@@ -0,0 +1,40 @@
+/*****************************************************************
+ *   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.cayenne.jpa.itest.ch2.entity;
+
+import java.io.Serializable;
+
+import javax.persistence.Basic;
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class SerializableEmbeddable1 implements Serializable {
+
+    @Basic
+    protected String property1;
+
+    public String getProperty1() {
+        return property1;
+    }
+
+    public void setProperty1(String property1) {
+        this.property1 = property1;
+    }
+
+}

Added: cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddedEntity.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddedEntity.java?rev=606712&view=auto
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddedEntity.java (added)
+++ cayenne/main/trunk/itests/jpa-chapter2/src/main/java/org/apache/cayenne/jpa/itest/ch2/entity/SerializableEmbeddedEntity.java Mon Dec 24 09:17:27 2007
@@ -0,0 +1,42 @@
+/*****************************************************************
+ *   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.cayenne.jpa.itest.ch2.entity;
+
+import javax.persistence.Embedded;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class SerializableEmbeddedEntity {
+
+    @Id
+    protected int id;
+
+    @Embedded
+    protected SerializableEmbeddable1 embeddable;
+
+    public SerializableEmbeddable1 getEmbeddable() {
+        return embeddable;
+    }
+
+    public void setEmbeddable(SerializableEmbeddable1 embeddable) {
+        this.embeddable = embeddable;
+    }
+
+}

Modified: cayenne/main/trunk/itests/jpa-chapter2/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter2/src/main/resources/META-INF/persistence.xml?rev=606712&r1=606711&r2=606712&view=diff
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter2/src/main/resources/META-INF/persistence.xml (original)
+++ cayenne/main/trunk/itests/jpa-chapter2/src/main/resources/META-INF/persistence.xml Mon Dec 24 09:17:27 2007
@@ -34,6 +34,8 @@
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.IdClassEntity</class>
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.EmbeddedIdEntity</class>
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.EmbeddedEntity</class>
+		<class>org.apache.cayenne.jpa.itest.ch2.entity.SerializableEmbeddedEntity</class>
+		<class>org.apache.cayenne.jpa.itest.ch2.entity.PropertyEmbeddedEntity</class>
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.EmbeddedImpliedEntity</class>
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.PropertyDefaultsPrimitiveEntity</class>
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.PropertyDefaultsWrapperEntity</class>
@@ -45,5 +47,7 @@
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.BidiOneToManyOwner</class>
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.BidiOneToManyOwned</class>
 		<class>org.apache.cayenne.jpa.itest.ch2.entity.Embeddable1</class>
+		<class>org.apache.cayenne.jpa.itest.ch2.entity.SerializableEmbeddable1</class>
+		<class>org.apache.cayenne.jpa.itest.ch2.entity.PropertyEmbeddable</class>
 	</persistence-unit>
 </persistence>

Modified: cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_5_EmbeddableTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_5_EmbeddableTest.java?rev=606712&r1=606711&r2=606712&view=diff
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_5_EmbeddableTest.java (original)
+++ cayenne/main/trunk/itests/jpa-chapter2/src/test/java/org/apache/cayenne/jpa/itest/ch2/_2_1_5_EmbeddableTest.java Mon Dec 24 09:17:27 2007
@@ -21,6 +21,10 @@
 import org.apache.cayenne.itest.jpa.EntityManagerCase;
 import org.apache.cayenne.jpa.itest.ch2.entity.Embeddable1;
 import org.apache.cayenne.jpa.itest.ch2.entity.EmbeddedEntity;
+import org.apache.cayenne.jpa.itest.ch2.entity.PropertyEmbeddable;
+import org.apache.cayenne.jpa.itest.ch2.entity.PropertyEmbeddedEntity;
+import org.apache.cayenne.jpa.itest.ch2.entity.SerializableEmbeddable1;
+import org.apache.cayenne.jpa.itest.ch2.entity.SerializableEmbeddedEntity;
 
 public class _2_1_5_EmbeddableTest extends EntityManagerCase {
 
@@ -36,5 +40,39 @@
         getEntityManager().getTransaction().commit();
 
         assertEquals("p1", getDbHelper().getObject("EmbeddedEntity", "property1"));
+    }
+
+    public void testPropertyEmbeddable() throws Exception {
+        getDbHelper().deleteAll("PropertyEmbeddedEntity");
+
+        PropertyEmbeddedEntity o1 = new PropertyEmbeddedEntity();
+        PropertyEmbeddable o2 = new PropertyEmbeddable();
+        o2.setProperty1("p1");
+        o1.setEmbeddable(o2);
+
+        getEntityManager().persist(o1);
+        getEntityManager().getTransaction().commit();
+
+        assertEquals("p1", getDbHelper().getObject("PropertyEmbeddedEntity", "property1"));
+    }
+
+    /**
+     * Check that Embeddables that implement Serializable interface are correctly enhanced
+     * and handled just as regular embeddables.
+     */
+    public void testSerializableEmbeddable() throws Exception {
+        getDbHelper().deleteAll("SerializableEmbeddedEntity");
+
+        SerializableEmbeddedEntity o1 = new SerializableEmbeddedEntity();
+        SerializableEmbeddable1 o2 = new SerializableEmbeddable1();
+        o2.setProperty1("p1");
+        o1.setEmbeddable(o2);
+
+        getEntityManager().persist(o1);
+        getEntityManager().getTransaction().commit();
+
+        assertEquals("p1", getDbHelper().getObject(
+                "SerializableEmbeddedEntity",
+                "property1"));
     }
 }

Modified: cayenne/main/trunk/itests/jpa-chapter2/src/test/resources/schema-hsqldb.sql
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter2/src/test/resources/schema-hsqldb.sql?rev=606712&r1=606711&r2=606712&view=diff
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter2/src/test/resources/schema-hsqldb.sql (original)
+++ cayenne/main/trunk/itests/jpa-chapter2/src/test/resources/schema-hsqldb.sql Mon Dec 24 09:17:27 2007
@@ -9,6 +9,8 @@
 insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('HelperEntity3', 1);
 insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('HelperEntity4', 1);
 insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('EmbeddedEntity', 1);
+insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('SerializableEmbeddedEntity', 1);
+insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('PropertyEmbeddedEntity', 1);
 insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('EmbeddedImpliedEntity', 1);
 insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('PropertyDefaultsPrimitiveEntity', 1);
 insert into AUTO_PK_SUPPORT (TABLE_NAME, NEXT_ID) VALUES ('PropertyDefaultsWrapperEntity', 1);
@@ -32,6 +34,8 @@
 create table IdClassEntity (id1 int not null, id2 int not null, property1 VARCHAR(100), primary key(id1, id2));
 create table EmbeddedIdEntity (id1 int not null, id2 int not null, property1 VARCHAR(100), primary key(id1, id2));
 create table EmbeddedEntity (id int not null, property1 VARCHAR(100), primary key(id));
+create table PropertyEmbeddedEntity (id int not null, property1 VARCHAR(100), primary key(id));
+create table SerializableEmbeddedEntity (id int not null, property1 VARCHAR(100), primary key(id));
 create table EmbeddedImpliedEntity (id int not null, property1 VARCHAR(100), primary key(id));
 create table PropertyDefaultsPrimitiveEntity (id INT not null, primitiveBoolean BOOLEAN, primitiveByte TINYINT, primitiveShort SMALLINT, primitiveChar CHAR(1), primitiveInt INT, primitiveLong BIGINT, primitiveFloat FLOAT, primitiveDouble DOUBLE, primary key(id));
 create table PropertyDefaultsWrapperEntity (id INT not null,  booleanWrapper BOOLEAN, byteWrapper TINYINT, shortWrapper SMALLINT, intWrapper INT, charWrapper CHAR(1), longWrapper BIGINT, floatWrapper FLOAT, doubleWrapper DOUBLE, primary key(id));