Restrict Hibernate to not recognize Validation ann

2019-09-05 19:25发布

问题:

I've been using hbm2ddl=update for Database creations until now. But right now we're going to remove the hbm2ddl property and use SchemaExport tool of Hibernate to create sql statement based on the Hibernate Annotated model classes. SchemaExport is also completed right now.

But when I'm making sure that hibernate generated database and SchemeExport generated schema sql are same by using mysqldiff tool on two databases (hibernate generated & SchemaExport generated), there are few columns which don't have same kind of constraints.

Suppose take the following Model

public class User {

    @Id
    // some generation strategy
    private int id;

    @Column(name = "first_name")
    @NotNull
    private String firstName;

    @Column(name = "someField", length = 50)
    @Size(min = 3, max = 20)
    private String someField;

}

Hibernate generated table has following column constraints.

`first_name` varchar(255) NOT NULL,
`someField` varchar(20) DEFAULT NULL

SchemeExport generated Schema has following constrinats.

`first_name` varchar(255) DEFAULT NULL,
`someField` varchar(50) DEFAULT NULL

So it's pretty obvious that Hibernate generation database takes the bean validation also into account during the ddl generation (wrong) while SchemaExport takes only the Database related annotation into account (right).

I know restricting the columns value using max = {x} but defining length = >max is pretty useless (same goes for @NotNull & nullable = true -> default), but my point is Validation annotations should only be used for Validations and Database releated annotation should only be used for ddl generation right..? Is there any way I could tell hibernate to not take Validation annotations into account during ddl generation..?

I'm using the following version of libraries

Hibernate 4.2.7
Spring 4.1.4 (Java based Bean configs, no hibernate.cfg.xml)

Similar kind of question but with exact opposite need. I can't find a suitable / explanable answer on why ddl generation recognizes it but not the SchemaExport tool or atleast the opposite.

Why does Hibernate Tools hbm2ddl generation not take into account Bean Validation annotations?

回答1:

You can disable the application of Bean Validation constraints to the DDL by setting the property _hibernate.validator.apply_to_ddl_ to false.