• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

not-null constraint in not working

 
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I use not-null="true" for my POJO properties in *.hbm.xml files.
Also, I have a web form which is supposed to collect data from the user (you know the story, this form will be posted to Struts action which it is in turn create the POJO and pass it to Hibernate persistence manager).
Well, I supplied no data in the form and pressed Submit, the result ??
I was totally shocked because a new blank row created in the database (in spite of not-null constraint).
By the way, I created my database schema using Hibernate generated schema (DDL file) and the not-null is included in the SQL script.
So ??
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate will not do the null check. The not-null property is meant to be used when you are creating database schemas out of the hbm files using tools.
 
Hussein Baghdadi
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So why a new blank row created in the database ?
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that the not-null constraint has been applied on the column in that table? Can you try using simple sql to insert a row into that table by passing a null value to the non-nullable column. If you are able to do that then it would mean that the non-nullable constraint hasnt been applied.
My point is, Hibernate should not come into picture in your scenario, since the null check will be done by your database.
[ February 14, 2007: Message edited by: Jaikiran Pai ]
 
Hussein Baghdadi
clojure forum advocate
Posts: 3479
Mac Objective C Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jaikiran Pai:
Are you sure that the not-null constraint has been applied on the column in that table? Can you try using simple sql to insert a row into that table by passing a null value to the non-nullable column. If you are able to do that then it would mean that the non-nullable constraint hasnt been applied.
[ February 14, 2007: Message edited by: Jaikiran Pai ]


Well, yes.
I viewed my tables schema via MySQL Control Center, and allow null check box is not checked (as expected).
By using this control center, I was able to insert a blank row, but by using raw SQL command, I wasn't !
(Well, this has to do with MySQL not Hibernate, I think)
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


(Well, this has to do with MySQL not Hibernate, I think)


Undoubtably.

Hibernate is designed to work with relational databases, and MySQL is not (by default) a relational database in the strictest sense. Though it might look like it supports all the RDBMS features of other databases, it doesn't. It has something called "SQL Modes" that basically determine whether it takes your request to apply a particular constraint seriously or not. Its default behaviour is to ignore many constraints, or treat them differently than you might expect. With the NOT NULL constraint for example if your data type is a character data type and you try to store a null value, rather than issuing a warning about a constraint violation it translates the NULL value into a blank string and inserts it anyway. See this page for a list of all inconsistancies.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic