Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

not-null constraint in not working

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • 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 ??
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why a new blank row created in the database ?
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • 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)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic