Hi there,
Found an intersting thing while I was playing with Hibernate & XMLEncoding...
The need is to manage persistent objects in an application, and also to be able to serialize/deserialize them to/from XML.
The direct idea is to use hibernate for persisting the objects, and XMLEncode them when you need it using the java.beans.XMLEncoder.
This actually won't work !
The problem arises when we come to java.util.Date objects ! These one should be XMLEncoded/Decoded easily, but actually they are not !
This is because when you load a persistent object instance, the java.util.Date properties actually reference java.sql.Timestamp objects !!!
These last ones are not well appreciated by the XMLEncoder (they are no java beans since they don't have no-args constructors !).
Does anyone have an idea to make it work ? I thought about deriving XMLEncoder/Decoder and add necessary behavior, but thought about posting here before...
Below is code snippet that shows the full story...
Cheers
Remi
-----------------------------
MyObject.java :
Code:
public class MyObject {
private int id;
private java.util.Date date;
// PK
public int getId() { return id; }
public void setId(int id) { this.id = id; }
// another prop
public java.util.Date getDate() { return date; }
public void setDate(java.util.Date theDate) { this.date = date; }
}
Test.java (in main) :
Code:
// get the session, open transac, etc...
...
// load a "MyObject" (there is a row for it in the db)
MyObject mo = (MyObject)session.load(12345);
XMLEncoder xe = new XMLEncoder(System.out);
xe.writeObject(d); // outputs an error message, see below
xe.close();
xe.flush();
The outputed message by XMLEncoder is :
Code:
java.lang.InstantiationException: java.sql.Timestamp
Continuing ...
java.lang.Exception: discarding statement XMLEncoder0.writeObject(Timestamp0);
Continuing ...
The outputed XML has everything in it excepted Date properties.