Announcement Announcement Module
No announcement yet.
Unexpected success when saving an obect with a null value Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unexpected success when saving an obect with a null value

    Hi. Just writing some test classes for a simple object. The object simply has NAME and DESCRIPTION attributes, as does the table its being stored into. The table definition has NAME as NOT NULL and DESCRIPTION as NULLABLE.

    When I test creating this object, one of the tests I do is try and create the object with a null name. Unfortunately what I am getting is - success! I was expecting a DataAccessException to be thrown. I know Firebird throws an exception because I tried the insert statement in native SQL. I was expecting Hibernate to throw an exception, and Spring to catch it and wrap it into a DataAccessException but it doesn't.

    It's worth noting that I didn't have not-null defined in the hibernate mapping because I expected the SQL exception to do the same job. I get my expected result when I do define this attribute on the hibernate property definition because Spring interrogates that first. It's just that I wasn't expecting to have to do that.

    Is there something I'm missing?

  • #2
    Try to do some logging at Hibernate level - there is a chance that the NAME string is initialized with "" and thus the NOT NULL requirement is met.


    • #3
      OK, here is where I'm at with this... And the reason I am asking this in the Spring forum and not the Hibernate forum will become clear.

      We have decided to code the not-null="true" directive on the hibernate mapping definition.

      Having done that we expect to get consistent behaviour whenever we try and do something stupid like set a not-null field to null! Whether it be via a create or via an update. Unfortunately this is not happening.

      While testing the dao layer, I am testing creating an object with a null name (this is the field that should be not null).

      As soon as I invoke create on my dao class, which calls hibernate's saveOrUpdate via Springs's getHibernateTemplate() I get an exception returned (this is good). This exception is of Spring's type ...hibernate3.HibernateSystemException with a cause set to an org.hibernate.PropertyValueException. OK, I can deal with that.

      However I am also testing an update that sets the name to null. When I invoke the update on my dao class, which also calls hibernate's saveOrUpdate via Springs's getHibernateTemplate() I do NOT get an exception unless I also flush the session. When I flush the session I don't get the exception scenario I described above, I get an org.hibernate.PropertyValueException returned.

      So I'm still unhappy that the same test using create and update do not operate in the same way (one I need to flush the Hibernate session the other I do not) and notwithstanding that I do not get consistent exceptions returned to me.

      All my dao calls are via Spring's hibernate template so it is appropriate that I ask this here. Why is this behaviour different?

      Thanks, Andrew