views:

38

answers:

1

Hi,

I'd like to use hibernate (orm) together with hibernate validator. Within the documentation we can find the following:

Out of the box, Hibernate Annotations (as of Hibernate 3.5.x) will translate the constraints you have defined for your entities into mapping metadata. For example, if a property of your entity is annotated @NotNull, its columns will be declared as not null in the DDL schema generated by Hibernate.

And this is my code:

test/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;
    }   
}

test/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"&gt;
<!-- 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"&gt;
<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>

and finally: 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();
        }   
    }
}

So, I would expect that the column myString would have a length of 36 but it has 255 (the default) and would be not null but it is null (the default).

The console prints the following:

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 (?)

Versions: hibernate-distribution-3.5.4-Final-dist and hibernate-validator-4.1.0.Final-dist

Does anyone see something wrong in my code? Any ideas?

Thanks in advance

+1  A: 

Well, you're not using annotations. Instead of:

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

Try:

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

I would personally fully move to annotations i.e. also use annotations to map entities (actually, I would fully move to JPA 2.0 and use the EntityManager API). But the above should work.

Pascal Thivent
AnnotationConfiguration (instead of Configuration) helped. Thanks!
@piotr286: You're welcome.
Pascal Thivent