liquibase-hibernate icon indicating copy to clipboard operation
liquibase-hibernate copied to clipboard

javax.validation.constraints are not included in diffs

Open bobbywarner opened this issue 4 years ago • 2 comments

Using a validation constraint annotation (i.e. javax.validation.constraints.NotNull) does not produce a NOT NULL constraint in the Liquibase migration. The same issue happens with using other javax constraints like @Size

liquibaseRuntime 'org.liquibase:liquibase-core:4.3.1'
liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:4.3.0.1'

The only way to have Liquibase generate NOT NULL in the migration is to use the @Column(nullable = false) annotation. This is bad though because then the check happens at the database layer as opposed to in the Java layer. The only way to have Java validate the constraint as well as have Liquibase generate the correct migration is to duplicate both annotations like this:

@Column(nullable = false)
@NotNull

Similar example for max size:

@Column(length = 100)
@Size(max = 100)

It's an issue with liquibase-hibernate5 as opposed to Hibernate because if I disable Liquibase and let Hibernate generate the schema, it will correctly generate the NOT NULL in the database with just @NotNull annotation as expected. The additional @Column annotation is only required when using Liquibase.

┆Issue is synchronized with this Jira Bug by Unito

bobbywarner avatar Feb 22 '21 15:02 bobbywarner

For some reason, my liquibase installation removes NOT NULL constraint even if I provide @Column(nullable = false) annotation.

@Column(nullable = false)
@NotNull
private String acronym;

<...>
    
-- changeset bohdan:1659912623805-1
ALTER TABLE public.teams ALTER COLUMN  acronym DROP NOT NULL;

bohdan-shulha avatar Aug 07 '22 23:08 bohdan-shulha

Ugh, oh. Just launched ./gradlew compileJava and DROP NOT NULL had gone. It is quite unobvious. Maybe, I'm not a good doc reader, who knows. 🤷

However, @NotNull is ignored, like always.

bohdan-shulha avatar Aug 07 '22 23:08 bohdan-shulha