Hibernate+DB2 : Adding new primitive data type member to an entity issue

By | January 25, 2016

This is an annoying issues of hibernate with DB2. Trying to add a new primitive type to an already existing entity will result in an error when updating the associated database table. As a result no update of the schema is done. Why is that ?
When adding to an entity a new primitive type for ex:


@Entity
public class Document
...
int processFlag=0;
...

Hibernate will generate the following schema update query:


alter table Document add column processFlag integer not null;

You can already spot the stupid issue that is causing the update to fail.
The Hibernate tries to add that column but because the table is not empty it fails (because the old entries will have a null value for the new column which is not allowed).


[1/21/16 17:18:31:969 EET] 000003eb SchemaUpdate Z org.hibernate.tool.hbm2ddl.SchemaUpdate execute HHH000388: Unsuccessful: alter table Document add column processFlag integer not null
[1/21/16 17:18:31:969 EET] 000003eb SchemaUpdate Z org.hibernate.tool.hbm2ddl.SchemaUpdate execute DB2 SQL Error: SQLCODE=-193, SQLSTATE=42601, SQLERRMC=PROCESSFLAG, DRIVER=3.63.123

The only way to solve this is outside Hibernate.

Execute the following SQL queries before deploying the new release containing the new entity column.

alter table Document add column processFlag integer not null default 0;
update Document set processFlag='ALLOW';

Advertisements