简体   繁体   English

休眠-错误:数据截断:错误的日期时间值

[英]Hibernate - ERROR: Data truncation: Incorrect datetime value

I'm using this code to insert data from one table to another, 我正在使用此代码将数据从一个表插入到另一个表,

Session session = HibernateUtil.getSessionFactory().openSession();
session.createSQLQuery(
                "INSERT INTO news_statistics (crime_type, crime_district, crime_date, crime_year, crime_yearquarter, crime_count) " +
                        "(SELECT crime_type,(SELECT district FROM location_district_mapper ldm WHERE ceg.location = ldm.location) , crime_date, YEAR (crime_date), CONCAT(YEAR (crime_date), ' - ', QUARTER(crime_date)), count(id) " +
                        "FROM crime_entity_group ceg " +
                        "WHERE crime_date >= '2012-01-01' AND crime_date <= '2014-12-31' " +
                        "GROUP BY crime_type, district, crime_date " +
                        "ORDER BY YEAR ('crime_date'))").executeUpdate();

This throws the error, 这引发了错误,

ERROR: Data truncation: Incorrect datetime value: 'crime_date'
Exception in thread "main" org.hibernate.exception.DataException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:69)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:211)
    at org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1310)
    at org.hibernate.internal.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:389)
    at com.cse10.analyzer.StatGenerator.generateStats(StatGenerator.java:22)
    at com.cse10.analyzer.Analyzer.main(Analyzer.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: 'crime_date'
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3556)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2643)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 10 more

But when I directly run the query with phpmyadmin the query runs well without showing any errors....! 但是当我直接使用phpmyadmin运行查询时,查询运行良好,没有显示任何错误....!

When I run only the select part of the query and list results with list() , it gives no errors. 当我只运行查询的select部分并使用list()列出结果时,它没有错误。 Because of that I can assume the problem is with the insert part of the query. 因此,我可以假设问题出在查询的insert部分。 When I remove the crime_date from the query, the query runs without showing any errors. 当我从查询中删除crime_date时,查询将运行而不会显示任何错误。

The type of crime_date in both tables is date and defined in following way in both hibernate xml files. 两个表中的crime_date的类型均为date并在两个hibernate xml文件中以以下方式定义。

<property name="crimeDate" type="date">
        <column name="crime_date" not-null="false"/>
</property>

The schemas are, 模式是

CREATE TABLE IF NOT EXISTS `crime_entity_group` (
  `id` int(11) NOT NULL,
  `crime_article_id` int(11) DEFAULT NULL,
  `crime_type` varchar(50) DEFAULT NULL,
  `crime_date` date DEFAULT NULL,
  `location` varchar(50) DEFAULT NULL,
  `district` varchar(20) DEFAULT NULL,
  `police` varchar(50) DEFAULT NULL,
  `court` varchar(50) DEFAULT NULL,
  `criminal` varchar(200) DEFAULT NULL,
  `victim` varchar(50) DEFAULT NULL,
  `victim_count` int(11) DEFAULT NULL,
  `possession` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `news_statistics` (
  `id` int(11) NOT NULL,
  `crime_type` varchar(50) DEFAULT NULL,
  `crime_district` varchar(20) DEFAULT NULL,
  `crime_date` date DEFAULT NULL,
  `crime_year` varchar(10) DEFAULT NULL,
  `crime_yearquarter` varchar(20) DEFAULT NULL,
  `crime_count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Anybody knows a solution? 有人知道解决方案吗?

Thank you in advance... 先感谢您...

You need to make sure the news_statistics.crime_date and the crime_entity_group.crime_date are of the same SQL type (eg DATE, TIME, TIMESTAMP, DATETIME). 您需要确保news_statistics.crime_datecrime_entity_group.crime_date具有相同的SQL类型(例如DATE,TIME,TIMESTAMP,DATETIME)。

If one is DATE and the other is DATETIME, your statement will fail because of the type mismatch constraint violation. 如果一个是DATE,另一个是DATETIME,则由于类型不匹配约束冲突,您的语句将失败。

From your exception message: 从您的异常消息中:

Incorrect datetime value: 'crime_date' 错误的日期时间值:“ crime_date”

It seems like the news_statistics.crime_date is a DATETIME column, while the crime_entity_group.crime_date is probably a DATE. 似乎news_statistics.crime_date是DATETIME列,而crime_entity_group.crime_date可能是DATE。

This expression 这个表达

CONCAT(YEAR (crime_date), ' - ', QUARTER(crime_date))

will be evaluated to something like '2015 - 1', and this is not valid value for columns of type DATE or DATETIME. 将被评估为类似于'2015-1'的值,对于DATE或DATETIME类型的列,该值无效。 Valid values for MySQL are like 'yyyy-mm-dd' , and also remove the white spaces arround the ' - ' (dash) sign. MySQL的有效值类似于'yyyy-mm-dd' ,并且还会删除'-'(破折号)符号周围的空格。

The problem was with ORDER BY YEAR ('crime_date')) . 问题出在ORDER BY YEAR ('crime_date')) My mistake.. 'crime_date' is a string, not a date. 我的错误。 'crime_date'是字符串,而不是日期。 That's why the error was thrown. 这就是引发错误的原因。 It should be changed to ORDER BY YEAR (crime_date)) . 应该将其更改为ORDER BY YEAR (crime_date))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM