限制Hibernate在DDL生成时不识别验证注释 [英] Restrict Hibernate to not recognize Validation annotations on DDL generation

查看:117
本文介绍了限制Hibernate在DDL生成时不识别验证注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在使用hbm2ddl=update进行数据库创建.但是现在,我们将删除hbm2ddl属性,并使用Hibernate的SchemaExport工具基于Hibernate带注释的模型类创建sql语句. SchemaExport现在也已完成.

但是,当我通过在两个数据库上使用mysqldiff工具(由hibernate生成的& SchemaExport生成的)来确保由hibernate生成的数据库和SchemeExport生成的模式sql是相同的时,几乎没有相同种类的列约束.

假设采用以下模型

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生成的表具有以下列约束.

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

SchemeExport生成的架构具有以下约束.

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

因此,很明显,在ddl生成期间(wrong),Hibernate生成数据库也考虑了bean验证,而SchemaExport仅考虑了与数据库相关的注释(right).

我知道使用max = {x}限制列值,但是定义length = >max几乎没有用(对于@NotNullnullable = true -> default也是一样),但是我的意思是,验证批注仅应用于验证和与数据库相关的注释仅应用于ddl生成.我有什么办法可以告诉Hibernate在ddl生成过程中不要考虑Validation注释.

我正在使用以下版本的库

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

类似的问题,但有完全相反的需求.对于为什么ddl生成可以识别它,但SchemaExport工具却不能识别它,我找不到合适的/可解释的答案.

为什么Hibernate Tools hbm2ddl生成是否不考虑Bean验证注释?

解决方案

您可以通过将_hibernate.validator.apply_to_ddl_属性设置为 false 来禁用Bean验证约束对DDL的应用.

>

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?

解决方案

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

这篇关于限制Hibernate在DDL生成时不识别验证注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆