2010-08-18 126 views
3

我想將hibernate(orm)與hibernate驗證器一起使用。我們可以發現以下文檔中:休眠驗證器

開箱,Hibernate註解 (如Hibernate的3.5.x的的)將轉化 你已經爲你的 實體爲映射元數據定義的約束。 例如,如果您的 實體的屬性註釋爲@NotNull,則其 列將在由 Hibernate生成的DDL模式中聲明爲非空 。

這是我的代碼:

測試/ MyClass.java

public class MyClass implements Serializable 
{ 
    private long id; 
    @Length(max=36) 
    @NotNull 
    private String myString; 

    public MyClass() {}; 

    public MyClass(String myString) 
    { 
     this.myString = myString; 
    } 

    public long getId() 
    { 
     return id; 
    } 

    public void setId(long id) 
    { 
     this.id = id; 
    } 

    public String getMyString() 
    { 
     return myString; 
    } 

    public void setMyString(String myString) 
    { 
     this.myString = myString; 
    } 
} 

測試/ MyClass.hbm.xml

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated 2010-08-02 21:13:04 by Hibernate Tools 3.3.0.GA --> 
<hibernate-mapping> 
    <class name="test.MyClass" table="my_table" > 
     <id name="id" type="long" unsaved-value="0"> 
      <column name="id" /> 
      <generator class="native"/> 
     </id> 
     <property name="myString" type="string"> 
      <column name="my_string"/> 
     </property> 
    </class> 
</hibernate-mapping> 

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
             "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost/htest</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">root</property> 
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <property name="hibernate.show_sql">true</property> 


    <!-- Mapping files --> 
    <mapping resource="test/myClass.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

最後:Test.java

public class Test 
{ 
    public static void main(String[] args) 
    {      
     Session session = null; 
     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 

     MyClass str1 = null; 
     Transaction tx = null; 
     try 
     { 
      session = sessionFactory.openSession(); 
      tx = session.beginTransaction(); 
      str1 = new MyClass("hello"); 
      session.save(str1); 
      tx.commit(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      session.close(); 
     } 
    } 
} 

所以,我希望該列的myString將有36的長度,但它具有255(默認值),將不爲空,但它爲空(默認)。

控制檯打印如下:

16:35:46,641 INFO SchemaUpdate:155 - Running hbm2ddl schema update 
16:35:46,642 INFO SchemaUpdate:167 - fetching database metadata 
16:35:46,644 INFO SchemaUpdate:179 - updating schema 
16:35:46,647 INFO DatabaseMetadata:119 - table not found: my_table 
16:35:46,649 INFO DatabaseMetadata:119 - table not found: my_table 
16:35:46,650 DEBUG SchemaUpdate:203 - create table my_table (id bigint not null auto_increment, my_string varchar(255), primary key (id)) 
16:35:46,755 INFO SchemaUpdate:217 - schema update complete 
16:35:46,793 DEBUG SQL:111 - insert into my_table (my_string) values (?) 
Hibernate: insert into my_table (my_string) values (?) 

版本:冬眠分佈-3.5.4-決賽 - 距離和休眠 - 驗證 - 4.1.0.Final - 距離

有誰看到的東西我的代碼錯了?有任何想法嗎?

在此先感謝

回答

2

那麼,你不使用註釋。相反的:

SessionFactory sessionFactory = new Configuration() 
    .configure() 
    .buildSessionFactory(); 

嘗試:

SessionFactory sessionFactory = new AnnotationConfiguration() 
    .configure() 
    .buildSessionFactory(); 

我個人完全移至註釋即還使用註解來映射實體(實際上,我會完全轉移到JPA 2.0,並使用EntityManager API)。但上述應該工作。

+0

AnnotationConfiguration(而不是配置)幫助。謝謝! – peter 2010-08-18 17:35:38

+0

@ piotr286:不客氣。 – 2010-08-18 17:50:54