Hibernate Date Support

23 views
Skip to first unread message

eggsy84

unread,
Nov 30, 2007, 7:36:04 AM11/30/07
to Google Web Toolkit
Hi all

I am aware of the problem using java.util.Date objects when using this
with Hibernate Date support in the fact that a Serialization exception
occurs because in effect we are really getting a java.sql.Timestamp
and I have seen that this issue has already been raised. Bruce Johnson
has advised that it will be fixed in GWT 1.5

Does anyone know when this release will be? Also has it been fixed in
the latest RC releases of GWT?

Looking at:

http://code.google.com/p/google-web-toolkit/downloads/list And
version 1.4.61 the release notes don't seem to indicate it has? Are
there any later RC versions thatn .61?

Fianlly does anyone know a workaround for this? I've looked into the
Hibernate4GWT but we have implemented our our persistent to DTO
methods and using the package would require a bit of an overhaul of
our existing code?

Thanks all

noon

unread,
Nov 30, 2007, 8:26:59 AM11/30/07
to Google Web Toolkit
Hello,

Maybe the simplest way to do it is to get the involved classes from
Hibernate4GWT (you will need the JSNI implementation and the
associated CustomSerializer) and add them to your project...

Regards
Bruno

Aragos

unread,
Nov 30, 2007, 8:55:54 AM11/30/07
to Google Web Toolkit
Hoi,

I wrote a simple custom UserType for exactly this problem that
translates the java.sql.Timestamp into Java Dates on deserialization
from the database, it's very simple:

public class DateTimeType extends TimestampType {
public Object get(ResultSet rs, String name) throws SQLException {
Timestamp timestamp = (Timestamp) super.get(rs, name);
if (timestamp == null) {
return null;
}
return new java.util.Date(timestamp.getTime());
}
}

Jut put the above class into your server side project and use it as
type in the hibernate mapping files:

<property name="registered"
type="com.company.some.pagacke.server.DateTimeType"
column="registered_on" />

Hope this helps!

Aragos

eggsy84

unread,
Nov 30, 2007, 10:09:34 AM11/30/07
to Google Web Toolkit
Oh right and then use simply java.util.Date within the DTO objects?

noon

unread,
Nov 30, 2007, 10:11:10 AM11/30/07
to Google Web Toolkit
@Aragos : The conversion of Timestamp to regular Date looses the
nanosecond part of the timestamp. That can be an issue if you are
using the timestamp as a version marker for example...

Bruno

eggsy84

unread,
Nov 30, 2007, 10:22:12 AM11/30/07
to Google Web Toolkit
Hi Aragos,

Thank you for this the nanosecond loss shouldn't be a problem but
thanks for letting my know Bruno.

My project uses annotations and I've solved it as such:

@Column(name="comment_date")
@Type(type="com.company.persistent.gwtproperty.DateTimeType")
public Date getCommentDate()
{
return commentDate;
}

public void setCommentDate(Date commentDate)
{
this.commentDate = commentDate;
}

----------

And your code above thanks again!
Reply all
Reply to author
Forward
0 new messages