hibernatespring-boothibernate-enversaudit-tables

Hibernate @Audited fails when saving dates


I have one entity that contains two dates, fromDate, and toDate, and if I dont audit it, it works perfectly, but, if I add the @Audited annotation, I get the following error:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1' for column 'from_date_mod' at row 1

My Entity is like this one:

@Entity
@Audited
public class MyEntity {

    @Id
    @GeneratedValue
    private BigInteger id;

    @NotNull
    private Date fromDate;

    private Date toDate;
    ....
}

And my liquibase script is this one:

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

So, it is something related to @Audited, because if I remove the annotation it works.

Do you know why I get the error? I would prefer not to change the java types of the field of MyEntity class, if possible.


Solution

  • The reason you get the error is because your liquidbase script is incorrect.

    The modified flags field support expects the ..._mod fields to be of a boolean-type that stores an indicator that represents true or false. This depends on the dialect as to what type of chosen for booleans, which might be some tinyint or bit, etc.

    Once you change that, I would expect the behavior should work.