-->
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.  [ 6 posts ] 
Author Message
 Post subject: mysql/hibernate date problem (3905 vs 2005)
PostPosted: Fri Jan 07, 2005 12:14 pm 
Newbie

Joined: Fri Jan 07, 2005 11:56 am
Posts: 6
Location: Manchester, England
I am using hibernate 2.1.4 and mysql 4.1.0

I have a java class(POJO) that hibernate persists to the database. The POJO contains a java.util.Date field.

If I set the java.util.Date field on the POJO and do System.out.println on it I get
Fri Jan 07 00:00:00 GMT 2005

Then if I persist the POJO using hibernate and then later refresh the POJO from the database, and then do a System.out.println on the date I get
3905-01-07 00:00:00

Ignoring the difference in formats there is a big problem here. The year is now 3905 instead of 2005. Strangely enough if I view the date in the database using a database tool (Navicat) the date appears as 07/01/2005.

Looking at the API documentation for java.util.Date it says "A year y is represented by the integer y - 1900". Now 3905 - 1900 = 2005 so it looks as though the phenomenom is something to do with this but I can't figure out exactly what is going on.

Any pointers welcome.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 12:31 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
What jdk are you using?

Are you sure that the second output is just 3905-01-07 00:00:00. Are you just doing a simple System.out.println(date) or are you using a date format? What happened to the GMT suffix?

Please post your mapping and the relevant code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 1:23 pm 
Newbie

Joined: Fri Jan 07, 2005 11:56 am
Posts: 6
Location: Manchester, England
I am using JDK 1.4.1_02

Here is the mapping for the POJO (the field I am talking about is startDate)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="film.hibernate.prcontract.PrContract" table="PrContract">
<id name="id" type="long" column="ID">
<generator class="native" />
</id>
<property name="refNo" type="java.lang.Long" >
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="startDate" type="java.util.Date" >
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="endDate" type="java.util.Date" >
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="licYears" type="java.lang.Integer" >
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="licMonths" type="java.lang.Integer" >
<meta attribute="use-in-tostring">true</meta>
</property>
<property name="notes" type="string" >
<meta attribute="use-in-tostring">true</meta>
</property>
<many-to-one name="film" class="film.hibernate.film.Film" column="filmId">
<meta attribute="use-in-tostring">true</meta>
</many-to-one>
<set name="commissions" inverse="true" cascade="all-delete-orphan">
<key column="prContractId" />
<one-to-many class="film.hibernate.commission.Commission" />
</set>
<set name="prContractProducers" inverse="true" cascade="all-delete-orphan" order-by="id">
<key column="prContractId" />
<one-to-many class="film.hibernate.prcontractproducer.PrContractProducer" />
</set>
<set name="filmMaterials" inverse="true" cascade="all-delete-orphan">
<key column="prContractId" />
<one-to-many class="film.hibernate.filmmaterial.FilmMaterial" />
</set>

</class>
</hibernate-mapping>

----------------------------------------------------------------------------------------------
I've rerun my test, this time using a DateFormat with pattern dd/MM/yyyy for printouts of date

After posting the datestring "07/01/2005" from web layer the startDate of the POJO prContract is updated
and prContract saved

log.info(dateFormat.format(prContract.getStartDate()));
session().saveOrUpdate(prContract);

what I see is:

07/01/2005

After the end of this transaction if I now retrieve what I've just saved

PrContract prContract = (PrContract)session.load(PrContract.class, prContractId );

and now look at the date again using log.info(dateFormat.format(prContract.getStartDate())) what I see is:

07/01/3905

Using PremiumSoft Navicat to look at the date entry in the mysqld database what I see is:

07/01/2005


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 1:44 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Try:

Code:
<hibernate-mapping>
   <class name="film.hibernate.prcontract.PrContract" table="PrContract">
   <id name="id" type="long" column="ID">
      <generator class="native" />
   </id>
   <property name="startDate" type="date" />
....
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 2:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Is your column in the database of the correct type?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 3:09 pm 
Newbie

Joined: Fri Jan 07, 2005 11:56 am
Posts: 6
Location: Manchester, England
drj, your advice worked!

changing

<property name="startDate" type="java.util.Date" >

to

<property name="startDate" type="date" >

did the job.

I generate my POJOs from this XML file using the appropriate hibernate tool. The POJO still looks the same, i.e. java.util.Date but now everything works fine.

Many thanks

Paul


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