Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does JPA change a datetime value from "0000-00-00 00:00:00" to null?

 
Rogerio Kioshi
Ranch Hand
Posts: 690
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a NOT NULL datetime column DATEAUTHORIZATION in a MySQL table. The records have the value "0000-00-00 00:00:00" in this field. I'm using JPA in my application, but when I read this datetime field, JPA changes it to null. If I want just update any other field (and not DATEAUTHORIZATION ) it raises an error complaining that the datetime field cannot be null.

Is there a way to make JPA transforms the value "0000-00-00 00:00:00" to a non nullable value?

How do I solve this problem?



 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is odd. Are you sure it is JPA and not your JDBC driver?

Try reading the value directly through JDBC, what is returned?

What JPA provider are you using?
 
Alex Armenteros
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That ยก's because the class used to represent datetimes in java is Timestamp and that class is incapable of represent that value


I've had the same problem in my hibernate program and solved it putting this in the url that connects to the DB

?zeroDateTimeBehavior=round

round puts the date to the nearest possible, have not checked which

round can be "null", to convert those dates to null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic