Home > OS >  HIbernate @Column(..) changes are not reflected in Database
HIbernate @Column(..) changes are not reflected in Database

Time:01-19

I have updated recently version of:

  • Hibernate to -> 5.6.14.Final
  • spring boot to -> 2.7.7
  • java to -> 17

And remarked that changes in @Column annotation are not reflected into DB. For example, if I add a column:

@Column(name = "visible_type_test")
private VisibleType visibleTypeTest = VisibleType.VISIBLE;

And then after running the project, decide to add nullable columnDefinition:

@Column(name = "visible_type_test", columnDefinition = "int DEFAULT 1", nullable = false)
private VisibleType visibleTypeTest = VisibleType.VISIBLE;

The changes are not reflected.

But if I drop the column and run the project again, it is successfully adds all default values (changes are reflected)

Do you know what could be the problem ? In application properties I have this configuration set:

spring.jpa.hibernate.ddl-auto=none

I tried switching to 'ddl-auto to update', but still it did not help. Tried also to remove@DynamicUpdate annotation from entity, but also did not help.

Why it was working all this time with the current settings and now it doesn't ? Was there any hibernate updates which I am not aware of ?

Edited: I am already using flyway. My problem is not about updating column name or deleting constraints. My problem is that unique constraints are not being added if I switch a column from nullable=true, to nullable=false. Also Default value is not being added to all existing values with columnDefi ition ... Default. I DID NOT NEEDED TO CREATE FLYWAY MIGRATION BEFORE IN ORDER TO DO THIS CHANGES. THEY WERE HAPPENING AUTOMATICALLY.

Found a similar post here also with no answer provided: Spring boot tables in database not updated when entity field is modified

CodePudding user response:

Looking at the code that is generating the ALTER TABLE statement, it appears to only add columns that are non-existing. It doesn't seem to alter existing columns.

That being said, AFAIK, the Hibernate team never adviced to use the DDL feature to manage your production tables but only use it for prototyping and/or quick testing.

For proper management you should use something like Flyway, more so when you migrate columns to non-null and before doing the conversion need to set a default to those columns, there are even more cases I can think off that would be problematic for altering (datatype changes, length, etc.).

That all being said, with Hibernate 5.6 and later this doesn't appear to work by design.

UPDATE: Went back all the way to the Hibernate 3.2 code in Github and even in that code it will only work for additions not changes to the column.

  • Related