Hi Horacio,

Horacio de Oro wrote:
Hi all!

We are experiencing problem with Date/Timestamp values since we are using cache:

java.lang.ClassCastException: java.util.Date
    at 
org.apache.ojb.broker.metadata.FieldTypeClasses$TimestampFieldType.copy(FieldTypeClasses.java:398)
    at 
org.apache.ojb.broker.cache.ObjectCacheTwoLevelImpl$CopyStrategyImpl.write(ObjectCacheTwoLevelImpl.java:664)
    at 
org.apache.ojb.broker.cache.ObjectCacheTwoLevelImpl.putToApplicationCache(ObjectCacheTwoLevelImpl.java:254)

We have java.util.Date in the objects, and TIMESTAMP in the DB
(PostgreSql 8.0). The mapping is configured with type "TIMESTAMP".

The problem is in the cast "Timestamp source = (Timestamp) fieldValue"...
See org.apache.ojb.broker.metadata.FieldTypeClasses

Current OJB version:

    public static class TimestampFieldType extends MutableFieldType
    {
        public Object copy(Object fieldValue)
        {
            Timestamp result = null;
            if(fieldValue != null)
            {
                Timestamp source = (Timestamp) fieldValue;   <--- HERE
                result = (Timestamp) source.clone();
            }
            return result;
        }
    }

I think that the original value should be casted to java.sql.Timestamp
only if the instance is a java.sql.Timestamp. I the others cases it
sould be casted to java.util.Date.

So, we have modified and compiled OJB... See our modified version:

        public Object copy(Object fieldValue)
        {
            Timestamp result = null;
            if(fieldValue != null)
            {
                if(fieldValue instanceof Timestamp) {
                    Timestamp source = (Timestamp) fieldValue;
                    result = (Timestamp) source.clone();
                } else {
                    java.util.Date source = (java.util.Date) fieldValue;
                    result = new Timestamp(source.getTime());
                }
            }
            return result;
        }


Please use the FieldConversion feature (in this case JavaDate2SqlTimestampFieldConversion) to allow OJB the conversion.
Specify a 'conversion' in field-descriptor:
http://db.apache.org/ojb/docu/guides/repository.html#field-descriptor-N105C6
http://db.apache.org/ojb/docu/guides/jdbc-types.html#field-conversion

Same procedure with your Date/sql-Date problem below - use a JavaDate2SqlDateFieldConversion.

If the problem still exists please let me know.

regards,
Armin


We have the same problems with Date, since the current OJB
implementation cast the object 'fieldValue' to java.sql.Date. To make
this work we have change the following method:

    public static class DateFieldType extends MutableFieldType
    {
        public Object copy(Object fieldValue)
        {
            Date source = (Date) fieldValue; <-- HERE
            return source != null ? new Date(source.getTime()) : null;
        }
    }

to this:

    public static class DateFieldType extends MutableFieldType
    {
        public Object copy(Object fieldValue)
        {
            java.util.Date source = (java.util.Date) fieldValue; <-- HERE
            return source != null ? new Date(source.getTime()) : null;
        }
    }


Are this changes to the OJB source ok? Or should we change the java
objects form java.util.Date to java.sql.Timestamp and to
java.sql.Date? We realy want to make the java objects independent of
the database columns types!

If the modification I've made are usefull to you, I could make patches
against the source of OJB tagged with OJB_1_0_4.

Thanks in advance!
Horacio

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to