0
我有一个实体包含两个日期,fromDate和toDate,如果我不审计它,它可以很好地工作,但是,如果我添加@Audited注释,会出现以下错误:保存日期时,Hibernate @Audited失败
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1' for column 'from_date_mod' at row 1
我的实体是像这样的:
@Entity
@Audited
public class MyEntity {
@Id
@GeneratedValue
private BigInteger id;
@NotNull
private Date fromDate;
private Date toDate;
....
}
而且我liquibase脚本是这一个:
databaseChangeLog:
- changeSet:
id: 1
author: Manuel
changes:
- createTable:
tableName: my_table
columns:
- column:
name: id
type: BIGINT
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: from_date
type: datetime
constraints:
nullable: false
- column:
name: to_date
type: datetime
- changeSet:
id: 2
author: Manuel
comment: Create Hibernate Envers audit table for my_table
changes:
- createTable:
tableName: my_table_aud
columns:
- column:
name: id
type: BIGINT
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: rev
type: BIGINT
constraints:
nullable: false
primaryKey: true
foreignKeyName: my_table_aud_revinfo_fk
referencedTableName: revinfo
referencedColumnNames: rev
- column:
name: revtype
type: TINYINT
defaultValue: null
- column:
name: from_date
type: datetime
defaultValue: null
- column:
name: from_date_mod
type: datetime
defaultValue: null
- column:
name: to_date
type: datetime
defaultValue: null
- column:
name: to_date_mod
type: datetime
defaultValue: null
所以,这与@Audited有关,因为如果我删除它的注释它的工作。
你知道我为什么得到错误吗?如果可能,我宁愿不更改MyEntity类的字段的Java类型。
嗨,纳罗斯,谢谢你的回答。我认为我没有正确理解你的答案。但是,假设我有其他表格,并且我将mod列定义为普通列,并且它正在工作,它只与日期类型一起失败。 – Manuelarte
然后,数据库可能会对数据类型进行内联强制,让您错觉一切都很好。显然日期是非常具体的情况,有独特的要求被认为是有效的,因此你看到的错误。举例来说,如果我在SQL Server上有'VARCHAR'字段,则相关的mod字段将具有'BIT'的数据类型来表示0 = false和1 = true。你在用什么方言? – Naros
Hi Nano,我正在使用MySQL方言:org.hibernate.dialect.MySQLDialect。 顺便说一句,我添加了跟踪日志记录,我看到了查询的值,并且你是完全正确的,它期望一个布尔值。 – Manuelarte