为什么Hibernate Tools hbm2ddl生成不考虑Bean Validation注解? [英] Why does Hibernate Tools hbm2ddl generation not take into account Bean Validation annotations?

查看:131
本文介绍了为什么Hibernate Tools hbm2ddl生成不考虑Bean Validation注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结:我正在使用Hibernate Tools 4.0.0-CR1和Hibernate 4.2(包括Hibernate Validator),但没有找到Bean验证。使用 hibernate.hbm2ddl.auto = create-drop 进行部署时正确生成模式

 < target name =schemaexport depends =jardescription =将生成的模式导出到数据库和文件> 
<路径id =lib.path>
< fileset refid =lib/>
< pathelement location =$ {jboss.home} /modules/org/apache/xerces/main/xercesImpl-2.9.1-jbossas-1.jar/>
< pathelement location =$ {jar.dir}/>
< / path>

< taskdef name =hibernatetoolclassname =org.hibernate.tool.ant.HibernateToolTask​​
classpathref =lib.path/>

< hibernatetool destdir =$ {basedir}>
< classpath refid =lib.path/>
< / hibernatetool>

< concat destfile =$ {dist.dir} /tic.sqlfixlastline =yes>
< filelist dir =$ {jar.dir}files =import.sql/>
< / concat>
< / target>

我的hibernate-console.properties如下:

  hibernate.connection.password = tic 
hibernate.connection.username = tic
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.url =的jdbc:在PostgreSQL://127.0.0.1:5432 / DB

hibernate.connection.provider_class =组织。 hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource =
hibernate.transaction.manager_lookup_class属性=

我仔细检查了罐子是否在我的lib.path中。



一个示例实体如下所示:

  @Entity 
public class Title实现Serializable {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Size(max = 50)@NotEmpty @Column(length = 50)
private String titlename;

@Size(max = 50)
private String shortTitle;





$ b

这里的问题是hbm2ddl会生成一个合适的varchar(50) titlename,而是shortTitle的通用varchar(255)。我遇到了与@NotNull类似的问题,以及基本上每个其他的bean验证注释。根据手册这应该只是工作[tm]。我需要区分验证api和java持久性api(jpa)(以及供应商特定的持久性api(jpa))。 )。 Hibernate考虑到JPA配置(和hibernate持久性api),当你不提供这样的配置时,这个过程就涉及到 Convention Over Configuration 原则。这就是为什么你得到 varchar(255) for

  @Size最大= 50)
私人字符串shortTitle;

它等于(我忽略了其他默认值)

  @Size(max = 50)
@Column(length = 255,nullable = true)
private String shortTitle;

验证API参与验证。检查字段是否填写正确。对于同一个字段,可能存在不同的验证规则。






更新



我的意思是这个 http:// beanvalidation .org / 1.0 / spec /#constraintsdefinitionimplementation-constraintdefinition-groups



对于其中一组,您验证其他约束。 / p>

例如

  @NotNull(groups = DefaultGroup.class)
@Null(groups = SecondGroup.class)
private String shortTitle;

然后

  Validator validator = Validation.buildDefaultValidatorFactory()。getValidator(); 
Set< ConstraintViolation< Title>> constraintViolations = validator.validate(title,DefaultGroup.class);
Set< ConstraintViolation< Title>> secondConstraintViolations = validator.validate(title,SecondGroup.class);


Summary: I'm using Hibernate Tools 4.0.0-CR1 and Hibernate 4.2 (including Hibernate Validator), but Bean Validations are not picked up. The schema is properly generated when deploying with hibernate.hbm2ddl.auto=create-drop.

But I prefer to generate my DDL via the following build.xml target:

<target name="schemaexport" depends="jar" description="Exports a generated schema to DB and files">
    <path id="lib.path">
        <fileset refid="lib" />
        <pathelement location="${jboss.home}/modules/org/apache/xerces/main/xercesImpl-2.9.1-jbossas-1.jar"/>
        <pathelement location="${jar.dir}" />
    </path>

    <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"
             classpathref="lib.path"/>

    <hibernatetool destdir="${basedir}">
        <classpath refid="lib.path"/>
        <jpaconfiguration persistenceunit="TIC" propertyfile="hibernate-console.properties" />
        <hbm2ddl outputfilename="${dist.dir}/db_ddl.sql" format="true"/>
    </hibernatetool>

    <concat destfile="${dist.dir}/tic.sql" fixlastline="yes">
        <filelist dir="${dist.dir}" files="db_ddl.sql" />
        <filelist dir="${jar.dir}" files="import.sql" />
    </concat>
</target>

My hibernate-console.properties is as follows:

hibernate.connection.password=tic
hibernate.connection.username=tic
hibernate.connection.driver_class=org.postgresql.Driver
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.url=jdbc:postgresql://127.0.0.1:5432/db

hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource=
hibernate.transaction.manager_lookup_class=

I double-checked that the jars are in my lib.path...

A sample entity looks like this:

@Entity
public class Title implements Serializable {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Size(max = 50) @NotEmpty @Column(length = 50)
    private String titlename;

    @Size(max = 50)
    private String shortTitle;
}

The problem here is that hbm2ddl generates a proper "varchar(50)" for "titlename" but a generic "varchar(255)" for "shortTitle". I encountered similar problems with @NotNull and basically every other bean validation annotation. According to the manual this should just work[tm]. What am I doing wrong?

解决方案

You need to distinguish validation api and java persistence api (jpa) (and vendor specific persistence api). Hibernate take into account JPA configuration (and hibernate persistence api) and when you do not provide such configuration then Convention Over Configuration principle is involved to this process. It is why you get varchar(255) for

@Size(max = 50)
private String shortTitle;

it equals to (i omitted other default values)

@Size(max = 50)
@Column(length = 255, nullable = true)
private String shortTitle;

Validation api is involved for validation purposes. To check if the fields properly filled. There can be exist the different validation rules for the same field.


Updated

I mean this http://beanvalidation.org/1.0/spec/#constraintsdefinitionimplementation-constraintdefinition-groups.

For one group you validate one constraint, for other group you validate other constraint.

For example

@NotNull(groups = DefaultGroup.class)
@Null(groups = SecondGroup.class)
private String shortTitle;

and then

    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<Title>> constraintViolations = validator.validate(title, DefaultGroup.class);
    Set<ConstraintViolation<Title>> secondConstraintViolations = validator.validate(title, SecondGroup.class);

这篇关于为什么Hibernate Tools hbm2ddl生成不考虑Bean Validation注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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