-->
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.  [ 13 posts ] 
Author Message
 Post subject: Date/Timestamp issue with MySQL
PostPosted: Thu Oct 02, 2003 3:12 pm 
Newbie

Joined: Thu Oct 02, 2003 2:57 pm
Posts: 9
Location: Sweden
I have looked through the old forum and the new and not found the answer to this problem (at least not one that worked) so if I am repeating an old question please know that I tried to find it.

I am using mysql 4.1 and hibernate 2.1 and I am trying to get effective dating on my objects by having 2 dates specifying effectiveFromDate and effectiveThroughDate.

<class name="some.random.Object">
<id name="id" column="uid" type="long" unsaved-value="0">
<generator class="native"/>
</id>

<property name="name" type="string" unique="true"/>
<property name="effectiveFromDate" type="timestamp"/>
<property name="effectiveThroughDate" type="timestamp"/>

<... more properties ...>
</class>

When I create new objects I set the effectiveFromDate to today's date unless otherwise specified. The effectiveThroughDate should be NULL unless otherwise specified to indicate that it is always effective. This works fine when I insert objects but when I load them again something (hibernate?) sets the date to the current time.

Query q = session.createQuery("FROM some.random.Object AS object WHERE ?>object.effectiveFromDate AND (object.effectiveThroughDate<? OR object.effectiveThroughDate is null)");
q.setTimestamp(0, date1);
q.setTimestamp(1, date2);
list = q.list();

I have tried using java.util.Date and datetime but with the same results. Can anyone suggest anything that could help me? I am loving hibernate in every way and it has worked perfectly until I tried using this.

// ulmus


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 3:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
This works fine when I insert objects but when I load them again something (hibernate?) sets the date to the current time.

Hibernate will not set the current date on a null date column. Make sure you are not doing this yourself in either 1) an Interceptor impl; 2) a DB trigger; 3) a DB column DEFAULT value.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 4:56 pm 
Newbie

Joined: Thu Oct 02, 2003 2:57 pm
Posts: 9
Location: Sweden
1) an Interceptor impl; Don't have any
2) a DB trigger; MySQL doesn't support triggers
3) a DB column DEFAULT value. There isn't one

This is completely baffling to me, but I promise that I don't set these values. It actually happens on every load, not only on the one I showed above but on a simple session.find("FROM some.random.Object") as well.
It might be MySQL since 4.1 is still in an alpha state but I haven't seen this problem before.

// ulmus


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 4:57 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Your column type should be DATETIME, not TIMESTAMP. Please refer to the MySQL documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 5:00 pm 
Newbie

Joined: Thu Oct 02, 2003 2:57 pm
Posts: 9
Location: Sweden
As I said in my original post I have tried timestamp, java.util.Date and datetime, all with the same result.

// ulmus


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 5:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
you tries java.util.Date as a -column- type?!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 5:22 pm 
Newbie

Joined: Thu Oct 02, 2003 2:57 pm
Posts: 9
Location: Sweden
gavin wrote:
you tries java.util.Date as a -column- type?!


From the docs on the hibernate site, section 4.1.9. property:

typename could be:

The name of a Hibernate basic type (eg. integer, string, character, date, timestamp, float, binary, serializable, object, blob).

The name of a Java class with a default basic type (eg. int, float, char, java.lang.String, java.util.Date, java.lang.Integer, java.sql.Clob).

The name of a subclass of PersistentEnum (eg. eg.Color).

The name of a serializable Java class.

The class name of a custom type (eg. com.illflow.type.MyCustomType).


So, yes, I tried using java.util.Date as type for my property.

I have tried the following configurations:
1)
<property name="effectiveThroughDate">
<column name="effectiveThroughDate" sql-type="datetime"/>
</property>
2)
<property name="effectiveThroughDate" type="timestamp"/>
3)
<property name="effectiveThroughDate" type="java.util.Date"/>

I am not saying this is a bug, what I am saying is that I am not able to figure out what I am doing wrong and I am asking for help cause I don't understand.

// ulmus


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 5:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
That is the Hibernate type.

What is the database -column- type?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 5:31 pm 
Newbie

Joined: Thu Oct 02, 2003 2:57 pm
Posts: 9
Location: Sweden
gavin wrote:
That is the Hibernate type.

What is the database -column- type?


It is datetime with no default and it allows null values.

// ulmus


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 5:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, basically the only conclusion is that you are mistaken somewhere.

I suggest you break out your debugger.

Of course Hibernate doesn't insert any default values.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2003 5:44 pm 
Newbie

Joined: Thu Oct 02, 2003 2:57 pm
Posts: 9
Location: Sweden
Thank you for trying at least. I will try and figure this out but the thing that stumps me is that I can manually set the value to null and I then run a VERY simple class that only reads from the database. And the value is reset to current time. I am starting to think it might be mysql. I don't understand how that can be but that's what I am thinking.

// ulmus


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 04, 2003 5:11 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 11:02 am
Posts: 20
Location: Montevideo, Uruguay
Hi ulmus,

I am working under the same conditions and had the same problem as U did with the dates (only when I loaded them Somehow the date was set in 3903!!)

I don't exactly know what solved the problem but here are my configurations files.

(I use java.util.GregorianCalendar for my date types)


<property name="publishDate">
<column name="PUBLISHDATE" sql-type="TIMESTAMP" not-null="true"/>
</property>
<property name="deadline">
<column name="DEADLINE" sql-type="TIMESTAMP" not-null="true"/>
</property>

Hope it helps.
My "semicomplete " configuration file are in the post:
Can't Correctly load Dates

Cheers
mata


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 04, 2003 10:27 pm 
Newbie

Joined: Thu Oct 02, 2003 2:57 pm
Posts: 9
Location: Sweden
I finally figured it out. I had put the transaction handling code in there even though I didn't actually had a transaction handler of any type. I put it in there in case I switched to another db eventually. Anyhow, cutting the transaction code out from around my load statement and everything works fine.

// ulmus


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