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.  [ 4 posts ] 
Author Message
 Post subject: Inserting with not-null properties and db defaults
PostPosted: Tue Aug 08, 2006 7:55 pm 
Newbie

Joined: Fri Apr 14, 2006 3:04 pm
Posts: 10
How is everyone else handling this?

I have a new entitiy I want to insert. In my mapping file, I've set the property/column to be not-null. For example:

<property name="SupplierContactEnabled" type="Boolean" >
<column name="SupplierContactEnabled" length="1" sql-type="tinyint" not-null="true" />
</property>

In the db, this has a default of 0 (false).

I have dynamic-insert set to true, but that really only applies to the SQL that gets generated. NHibernate objects when trying to save...that the not-null property has a null value.

The easy fix is to just omit the not-null attribute. Is there a better way?

Thanks in advance...
E


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 12:27 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i'm not sure i'm understanding your question, but if your db has a default value of 0 (false) then your class constructor should set the value to false as well:

Code:
class MyClass {
   private bool supplierContactEnabled;

   public MyClass() {
      this.supplierContactEnabled = false;
   }

   public virtual bool SupplierContactEnabled {
      get { return this.supplierContactEnabled; }
      set { this.supplierContactEnabled = value; }
   }
}


in this instance, even with dynamic SQL, the proper SQL would be generated and data integrity would be maintained. imho, a nullable bool is a misnomer. bool is either true or false, not true, false or null.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 4:40 pm 
Newbie

Joined: Fri Apr 14, 2006 3:04 pm
Posts: 10
Thanks for the reply, Devon.

I was afraid that might be the solution. It's not that bad putting it into the constructor...we'd have to hack it into our Codesmith templates...but can I follow up with another question?

The situation below occurs in literally thousands of places in our database for different datatypes. If we're not handling any specific exceptions on trying to commit, would it be *bad* to remove the not-null attribute? We'd still get the SQL exception bubbled up to us...

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 09, 2006 5:33 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Quote:
The situation below occurs in literally thousands of places in our database for different datatypes. If we're not handling any specific exceptions on trying to commit, would it be *bad* to remove the not-null attribute? We'd still get the SQL exception bubbled up to us...

well this is just my opinion, but i don't think your idea is *bad* if it saves you time in development. the caveat is that it should not increase time in support. thoughts if you proceed down this route:

1) you should probably use Nullables.NullableBoolean. you will be able to create robust error checking.

2) try and include some logic in your code that handles the error properly. i don't think you want SQL exceptions bubbling up to the UI.

just some quick thoughts.

-devon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.