cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r398607 - in /incubator/cayenne/jpa/trunk/cayenne-jpa/src: main/java/org/apache/cayenne/jpa/bridge/ main/java/org/apache/cayenne/jpa/conf/ main/java/org/apache/cayenne/jpa/map/ test/java/org/apache/cayenne/jpa/bridge/ test/java/org/apache/c...
Date Mon, 01 May 2006 14:43:38 GMT
Author: aadamchik
Date: Mon May  1 07:43:36 2006
New Revision: 398607

URL: http://svn.apache.org/viewcvs?rev=398607&view=rev
Log:
fixing temporal type conversion and defaults

Modified:
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAttribute.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessorTest.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/MockAnnotatedBean3.java
    incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java?rev=398607&r1=398606&r2=398607&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
Mon May  1 07:43:36 2006
@@ -17,6 +17,9 @@
 
 import java.beans.PropertyDescriptor;
 import java.lang.reflect.Field;
+import java.sql.Types;
+
+import javax.persistence.TemporalType;
 
 import org.apache.cayenne.jpa.JpaProviderException;
 import org.apache.cayenne.jpa.conf.EntityMapLoaderContext;
@@ -175,19 +178,35 @@
         @Override
         public boolean onStartNode(ProjectPath path) {
             JpaColumn jpaColumn = (JpaColumn) path.getObject();
+            JpaBasic jpaBasic = (JpaBasic) path.getObjectParent();
 
             ObjAttribute cayenneObjAttribute = (ObjAttribute) targetPath.getObject();
 
             DbAttribute dbAttribute = new DbAttribute(jpaColumn.getName());
 
-            // TODO, andrus, 4/28/2006 - per JPA spec, is there a way to override the type
-            // doing conversion implicitly???
-            dbAttribute.setType(TypesMapping.getSqlTypeByJava(cayenneObjAttribute
-                    .getJavaClass()));
+            // per JPA spec only date types are mapped explicitly...
+            int type;
+            TemporalType ttype = jpaBasic.getParent().getTemporal();
+            if (ttype != null) {
+
+                if (TemporalType.TIMESTAMP == ttype) {
+                    type = Types.TIMESTAMP;
+                }
+                else if (TemporalType.DATE == ttype) {
+                    type = Types.DATE;
+                }
+                else {
+                    type = Types.TIME;
+                }
+            }
+            else {
+                type = TypesMapping.getSqlTypeByJava(cayenneObjAttribute.getJavaClass());
+            }
+            dbAttribute.setType(type);
 
             dbAttribute.setMandatory(!jpaColumn.isNullable());
 
-            // TODO, andrus, 4/28/2006 - note that Cayenne DbAttribute's preceision is
+            // TODO, andrus, 4/28/2006 - note that Cayenne DbAttribute's precision is
             // really scale (and precision is not defined at all). Fix this in
             // DbAttribute.
             dbAttribute.setPrecision(jpaColumn.getScale());
@@ -219,16 +238,28 @@
 
             DbAttribute dbAttribute = new DbAttribute(jpaColumn.getName());
 
-            // TODO, andrus, 4/28/2006 - per JPA spec, is there a way to override the type
-            // doing conversion implicitly???
-
             JpaEntity jpaEntity = (JpaEntity) path.firstInstanceOf(JpaEntity.class);
             JpaId jpaId = (JpaId) path.firstInstanceOf(JpaId.class);
 
-            JpaPropertyDescriptor property = context.getLoadedDescriptor(
-                    jpaEntity.getClassName()).getProperty(jpaId.getName());
-
-            dbAttribute.setType(TypesMapping.getSqlTypeByJava(property.getType()));
+            // per JPA spec only date types are mapped explicitly...
+            int type;
+            if (jpaId.getTemporal() != null) {
+                if (TemporalType.TIMESTAMP == jpaId.getTemporal()) {
+                    type = Types.TIMESTAMP;
+                }
+                else if (TemporalType.DATE == jpaId.getTemporal()) {
+                    type = Types.DATE;
+                }
+                else {
+                    type = Types.TIME;
+                }
+            }
+            else {
+                JpaPropertyDescriptor property = context.getLoadedDescriptor(
+                        jpaEntity.getClassName()).getProperty(jpaId.getName());
+                type = TypesMapping.getSqlTypeByJava(property.getType());
+            }
+            dbAttribute.setType(type);
 
             dbAttribute.setMandatory(true);
             dbAttribute.setPrimaryKey(true);
@@ -319,6 +350,8 @@
                         + relationship.getTargetEntityName());
                 return false;
             }
+
+            cayenneRelationship.setTargetEntityName(jpaTargetEntity.getName());
 
             DbEntity cayenneSrcDbEntity = cayenneSrcEntity.getDbEntity();
 

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java?rev=398607&r1=398606&r2=398607&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
Mon May  1 07:43:36 2006
@@ -15,6 +15,10 @@
  */
 package org.apache.cayenne.jpa.conf;
 
+import java.util.Date;
+
+import javax.persistence.TemporalType;
+
 import org.apache.cayenne.jpa.JpaProviderException;
 import org.apache.cayenne.jpa.map.AccessType;
 import org.apache.cayenne.jpa.map.JpaAttribute;
@@ -113,14 +117,15 @@
             }
 
             JpaEntityMap root = (JpaEntityMap) path.getRoot();
-            if (root.getAccess() == AccessType.FIELD) {
-                for (JpaPropertyDescriptor candidate : descriptor.getFieldDescriptors())
{
+            if (root.getAccess() == AccessType.PROPERTY) {
+                for (JpaPropertyDescriptor candidate : descriptor
+                        .getPropertyDescriptors()) {
                     processProperty(entity, descriptor, candidate);
                 }
             }
+            // field is default...
             else {
-                for (JpaPropertyDescriptor candidate : descriptor
-                        .getPropertyDescriptors()) {
+                for (JpaPropertyDescriptor candidate : descriptor.getFieldDescriptors())
{
                     processProperty(entity, descriptor, candidate);
                 }
             }
@@ -181,6 +186,19 @@
                 JpaColumn column = new JpaColumn(AnnotationPrototypes.getColumn());
                 column.setName(jpaBasic.getParent().getName());
                 jpaBasic.setColumn(column);
+            }
+
+            if (jpaBasic.getParent().getTemporal() == null) {
+                JpaEntity entity = (JpaEntity) path.firstInstanceOf(JpaEntity.class);
+                JpaClassDescriptor descriptor = context.getLoadedDescriptor(entity
+                        .getClassName());
+                JpaPropertyDescriptor property = descriptor.getProperty(jpaBasic
+                        .getParent()
+                        .getName());
+
+                if (Date.class.equals(property.getType())) {
+                    jpaBasic.getParent().setTemporal(TemporalType.TIMESTAMP);
+                }
             }
 
             return true;

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAttribute.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAttribute.java?rev=398607&r1=398606&r2=398607&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAttribute.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaAttribute.java
Mon May  1 07:43:36 2006
@@ -42,7 +42,7 @@
     protected BasicSemantics basicSemantics;
     protected JpaColumn column;
     protected TemporalType temporal;
-    protected EnumType enumerated = EnumType.ORDINAL;
+    protected EnumType enumerated;
     protected String mapKey;
     protected String orderBy;
     protected boolean lob;

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java?rev=398607&r1=398606&r2=398607&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java
Mon May  1 07:43:36 2006
@@ -24,7 +24,7 @@
     protected String name;
     protected JpaColumn column;
     protected JpaGeneratedValue generatedValue;
-    protected TemporalType temporal = TemporalType.TIMESTAMP;
+    protected TemporalType temporal;
 
     @TreeNodeChild
     public JpaColumn getColumn() {

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java?rev=398607&r1=398606&r2=398607&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapMappingAssertion.java
Mon May  1 07:43:36 2006
@@ -16,6 +16,7 @@
 package org.apache.cayenne.jpa.bridge;
 
 import java.sql.Types;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
 
@@ -81,6 +82,10 @@
                 "Invalid SQL type: " + TypesMapping.getSqlNameByType(pk.getType()),
                 Types.INTEGER,
                 pk.getType());
+
+        DbAttribute column9 = (DbAttribute) table.getAttribute("column9");
+        assertNotNull(column9);
+        assertEquals(Types.DATE, column9.getType());
     }
 
     protected void assertObjAttributes(ObjEntity entity1) {
@@ -102,7 +107,7 @@
         assertNotNull(a3);
         assertEquals("attribute9", a3.getName());
         assertEquals("column9", a3.getDbAttributeName());
-        assertEquals(Integer.TYPE.getName(), a3.getType());
+        assertEquals(Date.class.getName(), a3.getType());
     }
 
     protected void assertObjRelationships(ObjEntity entity1) {
@@ -113,5 +118,6 @@
         assertNotNull(attribute5);
         assertFalse(attribute5.isToMany());
         assertEquals("attribute5", attribute5.getDbRelationshipPath());
+        assertEquals("MockCayenneTargetEntity2", attribute5.getTargetEntityName());
     }
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessorTest.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessorTest.java?rev=398607&r1=398606&r2=398607&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessorTest.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessorTest.java
Mon May  1 07:43:36 2006
@@ -17,6 +17,8 @@
 
 import java.io.Serializable;
 
+import javax.persistence.TemporalType;
+
 import junit.framework.TestCase;
 
 import org.apache.cayenne.jpa.map.JpaAttribute;
@@ -99,5 +101,12 @@
         assertEquals("pk", id.getName());
         assertNotNull(id.getColumn());
         assertEquals("pk", id.getColumn().getName());
+    }
+
+    public void testDate() throws Exception {
+        assertTrue(context.getConflicts().getFailures(
+                MockAnnotatedBean3.class.getDeclaredField("date")).isEmpty());
+        JpaAttribute date = entity.attributeForName("date");
+        assertEquals(TemporalType.TIMESTAMP, date.getTemporal());
     }
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/MockAnnotatedBean3.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/MockAnnotatedBean3.java?rev=398607&r1=398606&r2=398607&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/MockAnnotatedBean3.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/conf/MockAnnotatedBean3.java
Mon May  1 07:43:36 2006
@@ -16,6 +16,7 @@
 package org.apache.cayenne.jpa.conf;
 
 import java.util.Collection;
+import java.util.Date;
 
 import javax.persistence.Basic;
 import javax.persistence.Entity;
@@ -44,4 +45,7 @@
     @OneToMany
     // no collection type - must result in a failure
     protected Collection toBean2s2;
+    
+    // date w/o Temporal annotation must resolve to TIMESTAMP
+    protected Date date;
 }

Modified: incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java
URL: http://svn.apache.org/viewcvs/incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java?rev=398607&r1=398606&r2=398607&view=diff
==============================================================================
--- incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java
(original)
+++ incubator/cayenne/jpa/trunk/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/cayenne/MockCayenneEntity1.java
Mon May  1 07:43:36 2006
@@ -15,6 +15,8 @@
  */
 package org.apache.cayenne.jpa.entity.cayenne;
 
+import java.util.Date;
+
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -29,6 +31,8 @@
 import javax.persistence.OneToOne;
 import javax.persistence.QueryHint;
 import javax.persistence.Table;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
 import javax.persistence.Transient;
 import javax.persistence.Version;
 
@@ -78,5 +82,6 @@
     protected int attribute8;
 
     @Column(name = "column9")
-    protected int attribute9;
+    @Temporal(TemporalType.DATE)
+    protected Date attribute9;
 }



Mime
View raw message