3

我想将休眠(orm)与休眠验证器一起使用。在文档中,我们可以找到以下内容:

开箱即用的 Hibernate Annotations(从 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>

休眠.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 (?)

版本:hibernate-distribution-3.5.4-Final-dist 和 hibernate-validator-4.1.0.Final-dist

有人看到我的代码有问题吗?有任何想法吗?

提前致谢

4

1 回答 1

2

好吧,您没有使用注释。代替:

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

尝试:

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

我个人会完全转向注解,即也使用注解来映射实体(实际上,我会完全转向 JPA 2.0 并使用EntityManagerAPI)。但上述应该工作。

于 2010-08-18T17:15:50.987 回答