0

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.

Manuelarte
  • 1,658
  • 2
  • 28
  • 47

1 Answers1

1

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.

Naros
  • 19,928
  • 3
  • 41
  • 71
  • Hi Naros, thank you for answering. I do not think that I understood properly your answer. But, let's say that I have some other tables, and I defined the mod columns as the normal ones, and it is working, It is only failing with the date type. – Manuelarte Apr 12 '17 at 13:06
  • Then the database might be doing inline coercion of the data types giving you the illusion that everything is fine. Obviously dates are very specific cases that have unique requirements to be considered valid and thus you're seeing the error. As an example, if i have a `VARCHAR` field on SQL Server, the associated mod field would have the data type of `BIT` to represent 0=false and 1=true. What Dialect are you using? – Naros Apr 12 '17 at 15:57
  • Hi Nano, I am using the MySQL dialect: org.hibernate.dialect.MySQLDialect. Btw, I added the trace logging and I saw the values of the queries, and you are absolutely right, it expects a boolean. – Manuelarte Apr 12 '17 at 18:44
  • 1
    Awesome, just update your column data types and you should be all set. Good luck! – Naros Apr 12 '17 at 20:28