Home > database >  Spring boot upgrade causes tests to fail with hibernate exception "NULL not allowed for column
Spring boot upgrade causes tests to fail with hibernate exception "NULL not allowed for column

Time:07-13

For my Java Spring application, after upgrading Spring boot to version 2.7.0, my tests started failing with this error:

Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at com.core.SomeProject.Test.testSomething(SomeTest.java:224)
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: 
NULL not allowed for column "REV"; SQL statement:
insert into revinfo (rev, revtstmp) values (null, ?) [23502-212]

After looking around, a commonly suggested solution was to modify the create statement for the revinfo table to make rev be auto-incremented like this:

<addAutoIncrement tableName="REVINFO" columnName="REV" columnDataType="INTEGER"/>

If this is actually the solution, how do I configure this in my Spring application?

It looks like the revinfo table is automatically generated; it's not explicitly defined in the Spring project. The classes that have an @Audited tag above them have an audited table automatically generated for them, so I don't know how I can make that column autoincrement. Any ideas would be appreciated, thanks!

This is on hibernate-core version 5.6.9.Final and hibernate-envers version 5.3.20.Final.

CodePudding user response:

Your question is a bit vague but, the best I can suggest is try this:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int rev;

In your entity class.

CodePudding user response:

You can access the Database and alter the table to auto increment even on null values this works fine.

mysql> Create table employee(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, Name Varchar(10));
Query OK, 0 rows affected (0.16 sec)

When a nul value is passed into this,

mysql> Insert into employee(id, Name) values(NULL, 'Sarathi');
Query OK, 1 row affected (0.07 sec)

It wont consider the null and MySQL will return sequence number as seen below.

mysql> Select * from employee;
 ---- --------- 
| id | Name    |
 ---- --------- 
| 1  | Sarathi |
 ---- --------- 
1 row in set (0.00 sec)
  • Related