java.sql.Date - java.util.Date serialization issue

412 views
Skip to first unread message

Joshua Y.

unread,
Aug 21, 2006, 8:32:24 PM8/21/06
to Google Web Toolkit
Casting 'non-serializable' objects to 'serializable' objects alone is
not enough!

1. According to the serialization documentation for the GWT,
java.util.Date is serializable.
(
http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.DeveloperGuide.RemoteProcedureCalls.SerializableTypes.html
)
2. According to the java docs, java.sql.Date is a subclass of
java.util.Date.
( http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Date.html )
3. According to you-can-look-it-up-yourself you can validly cast a
subclass to the type of the superclass.

Intuitively, you should be able to send java.sql.Dates across the RPC
boundary simply by casting them. Unfortunately, the reflection calls
appear to look for the most specific type possible, not necessarily
whatever it was cast to last. (Maybe that's intended. Maybe I'm
totally off on my blind guess for what's causing this.) No matter how
I cast I java.sql.Date to java.util.Date, the call would always throw
an exception claiming I was trying to send a java.sql.Date. Eventually
I got fed up with that and created a new java.util.Date from the
java.sql.Date's getTime() method. Creating an entirely new object
solved the problem, but I'm assuming that's taking more time and
processing than a simple cast. If there's any way to make a sufficient
cast, I'd love to hear it.

Vivian Li

unread,
Aug 22, 2006, 2:24:07 AM8/22/06
to Google-We...@googlegroups.com

Joshua,
  Casting does not change an underlying object instance's true type. Consider the following:

public class Animal
{
   private String type;
   public Animal(String type) { this.type=type; }
   public String toString() { return "I am a "+type; }
}

public class Cat extends Animal
{
  private String color;
  public Cat(String color) { super("Cat"); this.color=color; }
  public String toString() { return "I am "+color+" and "+super.toString(); }
}

Now, if I sent a new Cat("Brown") across the network by casting it to (Animal), would an Animal object  be received, and the additional fields of Cat not transmitted? Would the client GWT JRE library not need to have a copy of the java.sql.Date implementation?

When you cast a java.sql.Date into a java.util.Date, it is still actually an instance of java.sql.Date, and therefore when you try to serialize it, you will sending a class to the GWT client which does not exist in the JRE emulation (no java.sql), and may not have compatible serializable fields (namely, doesn't implement IsSerializable).

All types which can be serialized in GWT must be present in the client JRE/sourcepath and understood by the GWT to be serializable (builtin types, IsSerializable classes)

-Vivian


Ralph Fiergolla

unread,
Jul 23, 2020, 10:17:25 AM7/23/20
to GWT Users
I know it is quite some time ago since you posted this, but I just now (2020!) ran into the very same issue (using the very same workaraound). Is there any real solution for this by now?

Michael Conrad

unread,
Jul 23, 2020, 10:34:18 AM7/23/20
to google-we...@googlegroups.com, Jason Carter
From experience here, passing java.sql.Date as java.util.Date is opening a barrel of woe.

Weird timezone issues start cropping up as java.sql.Date is not supposed to have an hour/minute/second component with meaning and would be better if timezone agnostic like LocalDate or LocalDateTime, but it isn't.

Keep in mind when getting back dates from the client, that the time zone of the client can cause you to generate a sql date that is off by a day + or -.

If you need to pass dates back and forth that are being used in database operations, make sure you take this into account, or just use wrapped strings that are parsed on the client and server to each's local time zone as needed.

Casting a subclass to a superclass is pretty much a no-op.
--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-web-toolkit/f971938a-06f4-403f-b737-161835da1bf8o%40googlegroups.com.

JonL

unread,
Jul 23, 2020, 8:31:48 PM7/23/20
to GWT Users
You could write a custom field serializer for java.sql.Date.  Serialize the information you need to recreate with the fidelity required for your use case.
Reply all
Reply to author
Forward
0 new messages