-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Proper way to handle date information
PostPosted: Tue Jan 18, 2005 4:43 pm 
Newbie

Joined: Thu Aug 05, 2004 11:00 pm
Posts: 7
Location: New York, NY
Hibernate version: 2.1

I'm attempting to sort entities using java.util.Date members, and implementing Comparable for use in "natural" sorting. I've run into a problem because I want to use plain old java.util.Date members in my domain model, but Hibernate constructs my domain objects with the java.sql.Timestamp subclass of java.util.Date.

You would think there shouldn't be a problem with this, but Java's OO implementation is, IMO, badly broken with regards to this use of inheritance. Sun's javadoc explicitly states that java.util.Date and java.sql.Timestamp are not interchangeable, even though their inheritance heirarchy would lead one to believe compatibility was intended. equals() breaks when you attempt to mix the two types, and java.sql.Timestamp's compareTo() throws a ClassCastException, of all things, when you attempt to compare two of them through java.util.Date's interface.

I'm hacking up my bean accessor methods and compareTo() as a workaround for this, but I was wondering if anyone here has thought of some more elegant and performant solution to this problem. I'd prefer not to expose java.sql.Timestamp in my domain model, but if this is the best way, then I'll do it.

Sorry for not including all my code and config files, but I think this is a generic enough type of problem that posting all the details would have only added to the length of my posting needlessly.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 9:54 pm 
Newbie

Joined: Thu Mar 10, 2005 9:46 pm
Posts: 2
Location: Baton Rouge, LA
I just came across this post, and thought I might be able to help in case you are still having the problem (or just another way of solving the problem). You can create the following UserType which will do the conversion back to java.util.Date:

Code:

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;


/**
* This class is used to solve the problem where Hibernate uses java.sql.Timestamp for fields that are
* declared as java.util.Date.  DateType maps java.util.Date fields as java.util.Date.
*/
public class DateType implements UserType
{
    public Object deepCopy(Object value) throws HibernateException
    {
        return (Date) value;
    }

    public boolean equals(Object x, Object y) throws HibernateException
    {
       if (x == y) {
          return true;
       }
       
       if (x == null) {
          return false;
       }
       
         return x.equals(y);
    }

    public boolean isMutable()
    {
        return true;
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException
    {
        // Start by looking up the value name
       Timestamp timestamp = (Timestamp) Hibernate.TIMESTAMP.nullSafeGet(rs, names[0]);
       if (timestamp == null) {
          return null;
       }
       
       return new Date(timestamp.getTime());
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException
    {
         Hibernate.TIMESTAMP.nullSafeSet(st, value, index);
    }

    public Class returnedClass()
    {
        return Date.class;
    }

    public int[] sqlTypes()
    {
        int[] typeList = {Types.TIMESTAMP};

        return typeList;
    }
}


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Fri Mar 11, 2005 11:38 am 
Newbie

Joined: Thu Aug 05, 2004 11:00 pm
Posts: 7
Location: New York, NY
Thanks. That's interesting. I haven't looked into extending UserType before, and I'll have to keep that idea in mind in the future.

_________________
When asked by a reporter what he thought of western civilization, Mahatma Ghandi once replied that he thought it would be a good idea.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.