-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate, XMLEncoder and Dates
PostPosted: Fri Nov 19, 2004 10:17 am 
Newbie

Joined: Fri Nov 19, 2004 9:56 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Hibernate, XMLEncoder and Dates
PostPosted: Mon Feb 01, 2010 2:42 pm 
Newbie

Joined: Tue Jan 05, 2010 3:37 pm
Posts: 1
I was wondering if you find a way around this. I am currently getting the same problem...

Thanks
Marc


Top
 Profile  
 
 Post subject: Re: Hibernate, XMLEncoder and Dates
PostPosted: Mon Feb 08, 2010 6:26 am 
Newbie

Joined: Fri Nov 19, 2004 9:56 am
Posts: 2
Hi Marc,

Nope I haven't investigated further, guess I found a workaround (to be honest I don't even remember in which project I found this problem)...

Good luck !

Cheers

Remi


Top
 Profile  
 
 Post subject: Re: Hibernate, XMLEncoder and Dates
PostPosted: Thu Feb 11, 2010 6:32 pm 
Newbie

Joined: Thu Feb 11, 2010 5:28 pm
Posts: 1
Salutations,

I'm pretty new to Hibernate as well, but I'm going to hazard a solution to your problem. I've attached an example below.

MyObject.java :
Code:
package org.datetest;

import java.util.Date;
import java.sql.Timestamp;

public class MyObject {

  private Date date;
  private int id;

  // PK
  public int getId() { return id; }
  public void setId(int id) { this.id = id; }

  // another prop
  public Date getDate() {
  return date;
  }

  public void setDate(Date date) {
  this.date = date;
  }

}


ModifiedTest.java

Code:
/**
*
* @author John Michael Sinclair
*
*/
package org.datetest;

import java.util.Date;
import java.sql.Timestamp;

public class ModifiedTest {

  public static void main(String[] args) {

  //Instantiate new Date class
  Date date = new Date();

  //Get the long millisecond value from Date() and convert it to a Timestamp
  Timestamp ts = new Timestamp(date.getTime());

  //Instantiate object model
  MyObject mo = new MyModifiedObject();

  //Call set method and cast ts to Date, after that print getDate()
  mo.setDate((Date)ts);
  System.out.println(mo.getDate());

  }

}


Persist your objects as Timestamps and cast them to (Date) at the application level if needed.

More info below-

http://forum.hibernate.org/viewtopic.php?f=1&t=925275
http://bugs.sun.com/bugdatabase/view_bu ... id=5103041


Regards,

John Michael.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.