liquibase / liquibase-hibernate

Liquibase Hibernate Integration
Apache License 2.0
271 stars 154 forks source link

Bean validation constraints not honored #149

Open llowrey opened 7 years ago

llowrey commented 7 years ago

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 span>@</spanLength(max=65535) liquibase assigns the column a type of varchar(255) but hibernate assigns it varchar(65535). Likewise, with I use span>@</spanNotNull 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

gdenchev commented 5 years ago

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?

llowrey commented 5 years ago

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