2013-07-01 58 views
4

摘要:我正在使用Hibernate工具4.0.0-CR1和Hibernate 4.2(包括Hibernate Validator),但沒有找到Bean驗證。使用hibernate.hbm2ddl.auto=create-drop進行部署時,正確生成的模式爲什麼Hibernate Tools hbm2ddl生成不考慮Bean Validation註釋?

但我更喜歡生成通過以下的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驗證註釋。根據the manual這應該只是工作[tm]。我究竟做錯了什麼?

回答

2

您需要區分驗證api和java持久性api(jpa)(以及供應商特定的持久性api)。 Hibernate考慮到JPA配置(和休眠持久性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); 
+0

我覺得我的牛頓與休眠是它*還提供了bean驗證的默認實現。你能提供一個「可以有不同的驗證規則的字段」的東西嗎?我的推理是,如果hibernate知道這個字段永遠不能包含超過50個字符,爲什麼要生成一個更廣泛的字符? – mabi

+0

啊,所以你說驗證規則不是絕對的,所以ddl生成器不能依賴它們。不知道這個,好點。 – mabi

-1

嘗試刪除@Size(最大值= 50),僅使用@Column(長度= 50)。還要將@Column(長度= 50)添加到變量shortTitle中。

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

/** User visible short version of the title. */ 
@Column(length = 50) 
private String shortTitle; 
+1

感謝捕捉C&P失敗。我應該更清楚地說明它:我希望Hibernate Tools拿起驗證註釋(它們需要留在那裏進行bean驗證),就像hibernate手冊所說的那樣。我很困惑,爲什麼hibernate(核心)尊重他們,而hibernate工具沒有。 – mabi

+0

我遇到了這個問題。看起來像生成的DDL不使用手冊中指定的JSR-303註釋。你找到解決方案嗎? – Thimmayya

相關問題