db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Horacio de Oro" <horaciode...@gmail.com>
Subject Copying objects in ojb.broker.metadata.FieldTypeClasses
Date Mon, 06 Mar 2006 17:46:16 GMT
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;
        }

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: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message