2010-08-18 5 views
3

최대 절전 모드 (orm)를 최대 절전 모드 유효성 검사기와 함께 사용하고 싶습니다. 우리는 다음을 찾을 수있는 문서 내에서 : 상자 밖으로hibernate validator

(최대 절전 모드 3.5.x의 기준) 최대 절전 모드 주석 는 매핑 메타 데이터에 당신의 실체에 대한 정의 제약을 변환합니다. 예를 들어, 엔티티의 속성에 @NotNull이 주석으로 지정된 경우 Hibernate에 의해 생성 된 DDL 스키마에서 해당 열이 null이 아닌 것으로 선언됩니다 ( ).

그리고 이것은 내 코드입니다 :

테스트/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 (기본값)을 가지고 있으며, null가 아닌 것하지만 (기본 null의 경우).

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

+0

@ piotr286 : 천만에. –