Reputation: 4896
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....!
When I run only the select
part of the query and list results with list()
, it gives no errors. Because of that I can assume the problem is with the insert
part of the query. When I remove the crime_date
from the query, the query runs without showing any errors.
The type of crime_date
in both tables is date
and defined in following way in both hibernate xml
files.
<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...
Upvotes: 1
Views: 7395
Reputation: 154190
You need to make sure the news_statistics.crime_date
and the crime_entity_group.crime_date
are of the same SQL type (e.g. DATE, TIME, TIMESTAMP, DATETIME).
If one is DATE and the other is DATETIME, your statement will fail because of the type mismatch constraint violation.
From your exception message:
Incorrect datetime value: '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.
Upvotes: 2
Reputation: 4896
The problem was with ORDER BY YEAR ('crime_date'))
. My mistake.. 'crime_date'
is a string, not a date. That's why the error was thrown. It should be changed to ORDER BY YEAR (crime_date))
.
Upvotes: 1
Reputation: 5502
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. Valid values for MySQL are like 'yyyy-mm-dd', and also remove the white spaces arround the ' - ' (dash) sign.
Upvotes: 1