cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r439628 - in /incubator/cayenne/main/trunk/core/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/ap...
Date Sat, 02 Sep 2006 18:28:57 GMT
Author: aadamchik
Date: Sat Sep  2 11:28:56 2006
New Revision: 439628

URL: http://svn.apache.org/viewvc?rev=439628&view=rev
Log:
fixing jpa enum defaults

Added:
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockEnum.java
Modified:
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaBasic.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaColumn.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaVersion.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java
    incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockTypesEntity.java

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/bridge/DataMapConverter.java
Sat Sep  2 11:28:56 2006
@@ -198,13 +198,11 @@
 
             if (attribute instanceof JpaBasic) {
                 JpaBasic basic = (JpaBasic) attribute;
-                dbAttribute.setType(attribute.getPropertyDescriptor().getJdbcType(
-                        basic.getTemporal()));
+                dbAttribute.setType(basic.getDefaultJdbcType());
             }
             else if (attribute instanceof JpaVersion) {
                 JpaVersion version = (JpaVersion) attribute;
-                dbAttribute.setType(attribute.getPropertyDescriptor().getJdbcType(
-                        version.getTemporal()));
+                dbAttribute.setType(version.getDefaultJdbcType());
             }
 
             dbAttribute.setMandatory(!jpaColumn.isNullable());
@@ -249,8 +247,7 @@
 
             JpaId jpaId = (JpaId) path.firstInstanceOf(JpaId.class);
 
-            dbAttribute.setType(jpaId.getPropertyDescriptor().getJdbcType(
-                    jpaId.getTemporal()));
+            dbAttribute.setType(jpaId.getDefaultJdbcType());
 
             dbAttribute.setMaxLength(jpaColumn.getLength());
             dbAttribute.setMandatory(true);
@@ -306,8 +303,7 @@
             src.setMandatory(false);
 
             src.setMaxLength(jpaTargetId.getColumn().getLength());
-            src.setType(jpaTargetId.getPropertyDescriptor().getJdbcType(
-                    jpaTargetId.getTemporal()));
+            src.setType(jpaTargetId.getDefaultJdbcType());
 
             DbEntity srcEntity = dataMap.getDbEntity(jpaJoin.getTable());
             srcEntity.addAttribute(src);

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/conf/EntityMapDefaultsProcessor.java
Sat Sep  2 11:28:56 2006
@@ -19,9 +19,12 @@
 
 package org.apache.cayenne.jpa.conf;
 
+import java.sql.Time;
+import java.sql.Timestamp;
 import java.util.Collection;
 import java.util.Date;
 
+import javax.persistence.EnumType;
 import javax.persistence.TemporalType;
 
 import org.apache.cayenne.jpa.JpaProviderException;
@@ -196,14 +199,43 @@
                 jpaBasic.setColumn(column);
             }
 
-            if (jpaBasic.getTemporal() == null) {
-                JpaEntity entity = (JpaEntity) path.firstInstanceOf(JpaEntity.class);
-                JpaClassDescriptor descriptor = entity.getClassDescriptor();
-                JpaPropertyDescriptor property = descriptor.getProperty(jpaBasic
-                        .getName());
+            JpaEntity entity = (JpaEntity) path.firstInstanceOf(JpaEntity.class);
+            JpaClassDescriptor descriptor = entity.getClassDescriptor();
+            JpaPropertyDescriptor property = descriptor.getProperty(jpaBasic.getName());
+
+            boolean typeKnown = false;
 
-                if (Date.class.equals(property.getType())) {
+            // process temporal type defaults
+            if (jpaBasic.getTemporal() != null) {
+                typeKnown = true;
+            }
+            else {
+                if (java.sql.Date.class.isAssignableFrom(property.getType())) {
+                    jpaBasic.setTemporal(TemporalType.DATE);
+                    typeKnown = true;
+                }
+                else if (Time.class.isAssignableFrom(property.getType())) {
+                    jpaBasic.setTemporal(TemporalType.TIME);
+                    typeKnown = true;
+                }
+                else if (Timestamp.class.isAssignableFrom(property.getType())) {
                     jpaBasic.setTemporal(TemporalType.TIMESTAMP);
+                    typeKnown = true;
+                }
+                else if (Date.class.isAssignableFrom(property.getType())) {
+                    jpaBasic.setTemporal(TemporalType.TIMESTAMP);
+                    typeKnown = true;
+                }
+            }
+
+            // process enum type defaults
+            if (!typeKnown) {
+                if (jpaBasic.getEnumerated() != null) {
+                    typeKnown = true;
+                }
+                else if (property.getType().isEnum()) {
+                    jpaBasic.setEnumerated(EnumType.ORDINAL);
+                    typeKnown = true;
                 }
             }
 

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaBasic.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaBasic.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaBasic.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaBasic.java
Sat Sep  2 11:28:56 2006
@@ -19,11 +19,14 @@
 
 package org.apache.cayenne.jpa.map;
 
+import java.sql.Types;
+
 import javax.persistence.Basic;
 import javax.persistence.EnumType;
 import javax.persistence.FetchType;
 import javax.persistence.TemporalType;
 
+import org.apache.cayenne.dba.TypesMapping;
 import org.apache.cayenne.util.TreeNodeChild;
 
 public class JpaBasic extends JpaAttribute {
@@ -44,6 +47,31 @@
         this.optional = basic.optional();
     }
 
+    /**
+     * Returns default JDBC mapping for this basic attribute.
+     */
+    public int getDefaultJdbcType() {
+
+        if (getTemporal() != null) {
+
+            if (TemporalType.TIMESTAMP == getTemporal()) {
+                return Types.TIMESTAMP;
+            }
+            else if (TemporalType.DATE == getTemporal()) {
+                return Types.DATE;
+            }
+            else {
+                return Types.TIME;
+            }
+        }
+        else if (getEnumerated() != null) {
+            return (getEnumerated() == EnumType.ORDINAL) ? Types.INTEGER : Types.VARCHAR;
+        }
+        else {
+            return TypesMapping.getSqlTypeByJava(getPropertyDescriptor().getType());
+        }
+    }
+
     public FetchType getFetch() {
         return fetch;
     }
@@ -84,7 +112,7 @@
     public void setLob(boolean lob) {
         this.lob = lob;
     }
-    
+
     public void setLobTrue(Object value) {
         setLob(true);
     }

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaColumn.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaColumn.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaColumn.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaColumn.java
Sat Sep  2 11:28:56 2006
@@ -141,4 +141,10 @@
     public void setUpdatable(boolean updateable) {
         this.updatable = updateable;
     }
+    
+    @Override
+    public String toString() {
+        String className = getClass().getName();
+        return className.substring(className.lastIndexOf('.') + 1) + ":" + name;
+    }
 }

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaId.java
Sat Sep  2 11:28:56 2006
@@ -19,8 +19,11 @@
 
 package org.apache.cayenne.jpa.map;
 
+import java.sql.Types;
+
 import javax.persistence.TemporalType;
 
+import org.apache.cayenne.dba.TypesMapping;
 import org.apache.cayenne.util.TreeNodeChild;
 
 public class JpaId extends JpaAttribute {
@@ -30,6 +33,28 @@
     protected TemporalType temporal;
     protected JpaTableGenerator tableGenerator;
     protected JpaSequenceGenerator sequenceGenerator;
+
+    /**
+     * Returns default JDBC mapping for this basic attribute.
+     */
+    public int getDefaultJdbcType() {
+
+        if (getTemporal() != null) {
+
+            if (TemporalType.TIMESTAMP == getTemporal()) {
+                return Types.TIMESTAMP;
+            }
+            else if (TemporalType.DATE == getTemporal()) {
+                return Types.DATE;
+            }
+            else {
+                return Types.TIME;
+            }
+        }
+        else {
+            return TypesMapping.getSqlTypeByJava(getPropertyDescriptor().getType());
+        }
+    }
 
     @TreeNodeChild
     public JpaColumn getColumn() {

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaPropertyDescriptor.java
Sat Sep  2 11:28:56 2006
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.jpa.map;
 
 import java.io.Serializable;
@@ -26,13 +25,9 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
-import java.sql.Types;
 import java.util.Collection;
 
-import javax.persistence.TemporalType;
-
 import org.apache.cayenne.jpa.JpaProviderException;
-import org.apache.cayenne.dba.TypesMapping;
 
 /**
  * A descriptor of a class property that may or may not be persistent.
@@ -106,27 +101,6 @@
         }
 
         return Void.TYPE.equals(targetEntityType) ? null : targetEntityType;
-    }
-
-    public int getJdbcType(TemporalType temporalHint) {
-        // per JPA spec only date types are mapped explicitly... everything else is guess
-        // per JDBC default...
-
-        if (temporalHint != null) {
-
-            if (TemporalType.TIMESTAMP == temporalHint) {
-                return Types.TIMESTAMP;
-            }
-            else if (TemporalType.DATE == temporalHint) {
-                return Types.DATE;
-            }
-            else {
-                return Types.TIME;
-            }
-        }
-        else {
-            return TypesMapping.getSqlTypeByJava(type);
-        }
     }
 
     public boolean isStringType() {

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaVersion.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaVersion.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaVersion.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/main/java/org/apache/cayenne/jpa/map/JpaVersion.java
Sat Sep  2 11:28:56 2006
@@ -18,8 +18,11 @@
  ****************************************************************/
 package org.apache.cayenne.jpa.map;
 
+import java.sql.Types;
+
 import javax.persistence.TemporalType;
 
+import org.apache.cayenne.dba.TypesMapping;
 import org.apache.cayenne.util.TreeNodeChild;
 
 public class JpaVersion extends JpaAttribute {
@@ -30,6 +33,28 @@
     @TreeNodeChild
     public JpaColumn getColumn() {
         return column;
+    }
+
+    /**
+     * Returns default JDBC mapping for this basic attribute.
+     */
+    public int getDefaultJdbcType() {
+
+        if (getTemporal() != null) {
+
+            if (TemporalType.TIMESTAMP == getTemporal()) {
+                return Types.TIMESTAMP;
+            }
+            else if (TemporalType.DATE == getTemporal()) {
+                return Types.DATE;
+            }
+            else {
+                return Types.TIME;
+            }
+        }
+        else {
+            return TypesMapping.getSqlTypeByJava(getPropertyDescriptor().getType());
+        }
     }
 
     public void setColumn(JpaColumn column) {

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/bridge/DataMapConverterTest.java
Sat Sep  2 11:28:56 2006
@@ -118,13 +118,22 @@
         DbAttribute timeColumn = (DbAttribute) typesTable.getAttribute("timeCalendar");
         assertNotNull(timeColumn);
         assertEquals(Types.TIME, timeColumn.getType());
-        
+
         DbAttribute dateColumn = (DbAttribute) typesTable.getAttribute("dateCalendar");
         assertNotNull(dateColumn);
         assertEquals(Types.DATE, dateColumn.getType());
-        
-        DbAttribute timestampColumn = (DbAttribute) typesTable.getAttribute("timestampCalendar");
+
+        DbAttribute timestampColumn = (DbAttribute) typesTable
+                .getAttribute("timestampCalendar");
         assertNotNull(timestampColumn);
         assertEquals(Types.TIMESTAMP, timestampColumn.getType());
+
+        DbAttribute defaultEnumColumn = (DbAttribute) typesTable
+                .getAttribute("defaultEnum");
+        assertNotNull(defaultEnumColumn);
+        assertEquals(
+                TypesMapping.getSqlNameByType(defaultEnumColumn.getType()),
+                Types.INTEGER,
+                defaultEnumColumn.getType());
     }
 }

Added: incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockEnum.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockEnum.java?rev=439628&view=auto
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockEnum.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockEnum.java
Sat Sep  2 11:28:56 2006
@@ -0,0 +1,23 @@
+/*****************************************************************
+ *   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.entity;
+
+public enum MockEnum {
+    a, b, c
+}

Modified: incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockTypesEntity.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockTypesEntity.java?rev=439628&r1=439627&r2=439628&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockTypesEntity.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jpa/src/test/java/org/apache/cayenne/jpa/entity/MockTypesEntity.java
Sat Sep  2 11:28:56 2006
@@ -37,4 +37,6 @@
 
     @Temporal(value = TemporalType.TIMESTAMP)
     protected Calendar timestampCalendar;
+    
+    protected MockEnum defaultEnum;
 }



Mime
View raw message