-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Not-null property in hbm not considered
PostPosted: Thu Sep 08, 2011 7:39 am 
Newbie

Joined: Wed May 24, 2006 5:47 pm
Posts: 14
Hi All

I'm using the following environment:
    Spring 3.0.5
    Hibernate 3.3.6

I created this hbm:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="it.TestRuolo" table="TEST_ROLE">
      <id name="roleId" type="int">
         <column name="ROLE_ID" length="10"/>
         <generator class="native"/>
      </id>
      <property name="roleDescription" not-null="false" type="string" length="50">
         <column name="ROLE_DESCRIPTION" />
      </property>

      <property name="roleLongDescription" type="string" unique="true" not-null="true" length="255">
         <column name="ROLE_LONG_DESCRIPTION" />
      </property>
   </class>
</hibernate-mapping>


this mapping class:
Code:
package it;

public class TestRuolo {
   
   private int roleId;
   private String roleDescription;
   private String roleLongDescription;
   public int getRoleId() {
      return roleId;
   }
   public void setRoleId(int roleId) {
      this.roleId = roleId;
   }
   public String getRoleDescription() {
      return roleDescription;
   }
   public void setRoleDescription(String roleDescription) {
      this.roleDescription = roleDescription;
   }
   public String getRoleLongDescription() {
      return roleLongDescription;
   }
   public void setRoleLongDescription(String roleLongDescription) {
      this.roleLongDescription = roleLongDescription;
   }
}

and this test class:
Code:
package test;

import it.SimpleDao;
import it.TestRuolo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHib {

   /**
    * @param args
    */
   public static void main(String[] args) {
      try {
         String[] springCtx = {"spring-aop-ctx.xml"};
         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(springCtx);
         SimpleDao sd = (SimpleDao)ctx.getBean("simpledao");
         TestRuolo tr = new TestRuolo();
         sd.saveTest(tr);
      } catch (Exception e) {
         
         e.printStackTrace();
      }

   }

}

I created this hibernate configuration file:
Code:
   <bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSourceHibernate">
      <property name="mappingResources">
         <list>
            <value>
               role.hbm.xml
            </value>
         </list>
      </property>
      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">
               org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">
               true
            </prop>
            <prop key="hibernate.generate_statistics">
               true
            </prop>
            <prop key="hibernate.format_sql">
               true
            </prop>
            <prop key="hibernate.hbm2ddl.auto">
               create
            </prop>
         </props>
      </property>
   </bean>


When i execute the test i see in the log file the following:
Code:
    create table TEST_ROLE (
        ROLE_ID integer not null auto_increment,
        ROLE_DESCRIPTION varchar(255),
        ROLE_LONG_DESCRIPTION varchar(255),
        primary key (ROLE_ID)
    )


As you can see when it creates the table the not-null instruction is not considered and when i execute the test i can save in the database a total empty object.

Can anybody tell me where i'm wrong?

Cheers,
Angelo


Top
 Profile  
 
 Post subject: Re: Not-null property in hbm not considered
PostPosted: Fri Sep 09, 2011 2:24 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The not-null attribute must be inside the <column> tag. The setting in <property> is only used if there is no <column> at all.


Top
 Profile  
 
 Post subject: Re: Not-null property in hbm not considered
PostPosted: Fri Sep 09, 2011 3:49 am 
Newbie

Joined: Wed May 24, 2006 5:47 pm
Posts: 14
nordborg wrote:
The not-null attribute must be inside the <column> tag. The setting in <property> is only used if there is no <column> at all.


Pardon me :) by checking better my hbm files i saw the error you told me :)

Thank you
Angelo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.