Problem saving java.util.Date in MySql using Hibernate

I debugged this problem for the last couple of hours without any success and decided that I would throw it in SO and see where this happens.

I am developing a Java program that stores data in a MySql database using Hibernate and the DAO / DTO pattern. There is a table memberprofilewith a column in my database firstLoginDate. In the database, the SQL type of this column is DateTime. Corresponding XML section of the Hibernate file:

<property name="firstLoginDate" type="timestamp">
    <column name="firstLoginDate" sql-type="DATETIME"/>
</property>

However, when I try to save Datein this table (in Java), the "date" part (year / month / day) is saved correctly, but the "time of day" part (hours: minutes: seconds) is not. For example, if I try to save a Java date representing 2009-09-01 14:02:23what ends up in the database instead 2009-09-01 00:00:00.

I have already confirmed that my own code does not stomp on the time component; as far as I can see the source code (when debugging) the time component remains true. However, after making the changes, I can check the corresponding line using MySql Query Browser (or simply extract it from the database in my Java code), and indeed, the time component is missing. Any ideas?

I tried to save java.sql.Timestampinstead java.util.Date, but the problem remained. Also, I have a very similar column in another table that does not show this behavior at all.

I expect you guys to have questions, so I will edit this as needed. Thanks!


Edit @Nate:

...
MemberProfile mp = ...
Date now = new Date();
mp.setFirstLoginDate(now);
...

memberprofilepretty much a wrapper class for DTO; setting the first entry date sets the DTO field and then commits the changes.


Edit 2: It seems this only happens on my machine. I have already tried rebuilding the table schema and destroying my entire local source and reinstalling from CVS without any improvements. Now I'm really at a standstill.


3: MySql, .

+3
4

( ), :

<property name="firstLoginDate" type="timestamp">
    <column name="firstLoginDate" length="19"/>
</property>

.

Edit:

, ...

  • , mysql , .
  • hibernate . , .
+1

, / - , , , .

, :

@Column(name="COLUMN_NAME", length=11)
0

, , JodaTime DateTime, , , Hibernate Hibernate

, :

@org.hibernate.annotations.Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime" )  @Column (name = "date" )

This works fine for me, and also generates proper sql schema construction

This works fine in MySQL

0
source

Use TemporalType.TIMESTAMP next to your temporary registration.

Please see the example below.

@Temporal(TemporalType.TIMESTAMP)
public Date getCreated() {
    return this.created;
}
-1
source

Source: https://habr.com/ru/post/1716630/


All Articles