摘要:我正在使用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]。我究竟做錯了什麼?
我覺得我的牛頓與休眠是它*還提供了bean驗證的默認實現。你能提供一個「可以有不同的驗證規則的字段」的東西嗎?我的推理是,如果hibernate知道這個字段永遠不能包含超過50個字符,爲什麼要生成一個更廣泛的字符? – mabi
啊,所以你說驗證規則不是絕對的,所以ddl生成器不能依賴它們。不知道這個,好點。 – mabi