-->
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+MySQL+DateTime
PostPosted: Fri Mar 05, 2010 9:54 pm 
Newbie

Joined: Fri Mar 05, 2010 9:46 pm
Posts: 4
I have a table in Mysql with a column of type datetime. If this column is null or it is "0000-00-00 00:00:00", it make a exception for sql gramatic. If this column have a good date, it have not exceptions.
I have tried with type date and timestamp on "hbm.xml".

How can i resolve it?


Top
 Profile  
 
 Post subject: Re: Hibernate+MySQL+DateTime
PostPosted: Wed Mar 17, 2010 9:10 am 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Same problem here. I'm on a legacy MySQL database. The datetime column is not nullable, but some of the data was inserted with null values. So from Googling I found that MySQL inserts 0000-00-00 00:00:00 to force it to be not-null. However that can't be represented in Java by the looks of things.

Anyone know how to deal with this? I'm using JPA (with Hibernate) and have tried a few different annotations and various getter/setter methods. No joy. Would Interceptors be useful here to perform some kind of conversion (from all-zeros to a simple null)?

Really appreciate any tips people could pass on.


Top
 Profile  
 
 Post subject: Re: Hibernate+MySQL+DateTime
PostPosted: Wed Mar 17, 2010 11:27 am 
Newbie

Joined: Fri Mar 05, 2010 9:46 pm
Posts: 4
I have solved it, but i don't remember it.
It need set a parameter on url, it maybe to be "zeroDateTimeBehavior=convertToNull".


Top
 Profile  
 
 Post subject: Re: Hibernate+MySQL+DateTime
PostPosted: Wed Mar 17, 2010 12:18 pm 
Beginner
Beginner

Joined: Fri Nov 24, 2006 10:33 am
Posts: 42
Hey, thanks. I've just looked it up in the Connector/J docs and it sounds like it will work for me too. Cheers

For what its worth, my solution (which also works, but not as neat and postentially dangerous!) was to 'fix' the database using:

// change the column to allow null values
ALTER TABLE transactions modify ExecuteAt DATETIME DEFAULT NULL;

// change any existing data so that zeros are set to null
UPDATE transactions SET ExecuteAt = null WHERE ExecuteAt = '0000-00-00 00:00:00';


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.