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

Bean validation constraints not honored

Open llowrey opened this issue 7 years ago • 2 comments

I'm using liquibase-maven-plugin version 3.5.3 and running liquibase:diff with the following reference:

referenceUrl=hibernate:spring:com.example.package.model?dialect=org.hibernate.dialect.MySQL57InnoDBDialect

When I have a String field annotated with @Length(max=65535) liquibase assigns the column a type of varchar(255) but hibernate assigns it varchar(65535). Likewise, with I use @NotNull liquibase does not apply a "nullable: false" constraint but hibernate does.

Hibernate does apply the bean validation annotation logic to construct correct metadata during EntityManagerFactoryBuilderImpl.build() but somehow that metadata is lost. When the plugin creates metadata through the MetadataSources process the bean validation annotations are not processed.

I believe the process by which metadata is being extracted is unnecessary. You can get the metadata directly from EntityManagerFactoryBuilderImpl without going through the MetadataSources process. The metadata you get from the builder has the annotations processed as expected.

EntityManagerFactory emf = builder.build();
for (Table table : mi.collectTableMappings()) {
    System.out.printf("Table '%s'%n", table.getName());
    Iterator<?> i = table.getColumnIterator();
    while (i.hasNext()) {
        Column col = (Column)i.next();
        System.out.printf("\tcol:%s %s %d %b%n", col.getName(), col.getSqlType(dialect, null), col.getLength(), col.isNullable());
    }
}

Has this technique already been considered? Has anyone else experienced the issue with bean validation constraints? Does it work for everyone else and I doing something wrong?

┆Issue is synchronized with this Jira Bug by Unito

llowrey avatar Apr 21 '17 23:04 llowrey

I am a bit late to the party, but I have the same problem here. I am using the hibernate:spring:packageNames url format, and @NotNull and @Size are not honored.

Unfortunately, I have merged your pull request and tried, but cannot get it to work. Annotations are still not honored ... Can you please post your working configuration?

gdenchev avatar Nov 22 '18 11:11 gdenchev

I never came up with a complete solution and ended up solving my problem with a non-liquibase solution.

llowrey avatar Nov 22 '18 18:11 llowrey