-->
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.  [ 8 posts ] 
Author Message
 Post subject: Design tip: Boolean or boolean ?
PostPosted: Tue Nov 09, 2010 6:01 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hi,

I wonder which way to go with the [B/b]oolean choice in Hibernate.

My Dao layer is built using the Criteria API.

At first, I took the boolean way, the primitive type.

It worked fine.

I could have a Criteria that looked like:

Code:
   public List<Webpage> findAllNotInGarbage() {
      Criteria criteria = getSession().createCriteria(Webpage.class);
      criteria.add(Restrictions.eq("garbage", false));
      criteria.addOrder(Order.asc("name"));
      return criteria.list();
   }


But then, I read that it's better to take the Boolean way, the complex type.

And so, the property is then nullable.

A "false" might contain the false value or the null value.

A "true" does contain the true value.

Therefore, when checking against "false" values, the Criteria needs to check for both false and null values.

Code:
   public List<Webpage> findAllNotInGarbage() {
      Criteria criteria = getSession().createCriteria(Webpage.class);
      criteria.add(Restrictions.or(Restrictions.eq("garbage", false), Restrictions.isNull("garbage")));
      criteria.addOrder(Order.asc("name"));
      return criteria.list();
   }


And when retrieving a value as in:

Code:
webpage.getGarbage();


the following assertion might be failing if the value is null instead of false:

Code:
assertEquals(false == webpage.getGarbage());


and this one might succeed:

Code:
assertNull(webpage.getGarbage());


In the second way, I therefore end up with a more complex Criteria, and an uncertainty, that is, two possible values for a "false", either false or null.

What do you think ?

Which [B/b]oolean way would you go ?

Cheers,


Top
 Profile  
 
 Post subject: Re: Design tip: Boolean or boolean ?
PostPosted: Tue Nov 09, 2010 6:21 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I therefore end up with a more complex Criteria, and an uncertainty, that is, two possible values for a "false", either false or null.

What do you think ?


Use a 'boolean'. Only use 'Boolean' if you have a use case that needs null values that are handled different from both 'true' and 'false'. As you said, everything gets more complicated if you have both 'null' and 'false' mean the same thing.

My best example of a use case is file validation were the validation is optional since it is not implemented for all file types. I have used a Boolean were: false=the file has been checked and is not valid, true=the file has been checked and is valid, null=the file has not been checked.


Top
 Profile  
 
 Post subject: Re: Design tip: Boolean or boolean ?
PostPosted: Tue Nov 09, 2010 6:54 pm 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Yes, use "boolean", unless your database column is really nullable.


Top
 Profile  
 
 Post subject: Re: Design tip: Boolean or boolean ?
PostPosted: Wed Nov 10, 2010 3:41 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Okay, thanks for these explanations. I shall revert to the boolean primitive type.


Top
 Profile  
 
 Post subject: Re: Design tip: Boolean or boolean ?
PostPosted: Wed Nov 10, 2010 4:36 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
As an added note, I specified the boolean columns as being not null so as to avoid any NULL values stored in these boolean columns.

Code:
`locked` tinyint(1) NOT NULL,


This was needed because the schema is also used by a PHP legacy application. Having NULL values in these boolean columns was making PHP return false on the condition:
Code:
locked != '1'

as the not equal operator cannot be run properly on a NULL value.

Luckily, Hibernate is kind enough not to require the mapping to have a not-null='true' attribute and thus allowing the object boolean property not being set. When creating a new object, and persisting it, I therefore don't need to explicitly set all boolean properties to false, or true, I can leave them unspecified.


Top
 Profile  
 
 Post subject: What do other experienced users think?
PostPosted: Sat Dec 04, 2010 9:19 am 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
For numbers (Long, Integer etc) I always use Wrappers, but for booleans I always find it hard to decide which one to choose. From a practical perspective, I would choose Boolean. When using Boolean/Wrappers you can easily get NPE's when using logical conditions, like Stephaneeybert says.
(I also had a problem once where a not-null Hibernate constraint on a wrapper caused a MySQL query that performed really slow compared to without the constraint.)

The book "Java Persistence with Hibernate" uses wrappers for numbers. In the whole book, there is only one example where they use a primitive boolean (for the "admin" property of the "User" class), but the Boolean wrapper isn't used at all for entities.

I'm wondering what other experienced users think, especially those from the Hibernate team. What would be the recommended/best-practice, Anyone?


Top
 Profile  
 
 Post subject: Re: Design tip: Boolean or boolean ?
PostPosted: Mon Dec 06, 2010 7:19 am 
Beginner
Beginner

Joined: Wed Jan 07, 2009 7:07 pm
Posts: 26
How about enum ?


Top
 Profile  
 
 Post subject: Re: Design tip: Boolean or boolean ?
PostPosted: Mon Dec 06, 2010 7:40 am 
Newbie

Joined: Tue Sep 21, 2004 11:21 am
Posts: 8
i use wrappers if and only if the column can be null. otherwise always use primitieves.


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