4

总结:我正在使用 Hibernate Tools 4.0.0-CR1 和 Hibernate 4.2(包括 Hibernate Validator),但没有选择 Bean Validations。使用. _ _hibernate.hbm2ddl.auto=create-drop

但我更喜欢通过以下 build.xml 目标生成我的 DDL:

<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>

我的 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=org.hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource=
hibernate.transaction.manager_lookup_class=

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

示例实体如下所示:

@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;
}

这里的问题是 hbm2ddl 为“titlename”生成一个正确的“varchar(50)”,但为“shortTitle”生成一个通用的“varchar(255)”。我遇到了@NotNull 和基本上所有其他bean 验证注释的类似问题。根据手册,这应该只是工作[tm]。我究竟做错了什么?

4

2 回答 2

4

您需要区分验证 api 和 java 持久性 api (jpa)(以及供应商特定的持久性 api)。Hibernate 会考虑 JPA 配置(和 hibernate 持久性 api),当您不提供此类配置时Convention Over Configuration,此过程将涉及原则。这就是为什么你varchar(255)得到

@Size(max = 50)
private String shortTitle;

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

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

验证 api 用于验证目的。检查字段是否正确填写。同一字段可以存在不同的验证规则。


更新

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

对于一个组,您验证一个约束,对于另一个组,您验证另一个约束。

例如

@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);
于 2016-01-10T03:04:13.140 回答
-1

尝试删除@Size(max=50),仅使用@Column(length = 50)。还将@Column(length = 50) 添加到变量 shortTitle。

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

/** User visible short version of the title. */
@Column(length = 50)
private String shortTitle;
于 2013-07-01T08:36:35.563 回答