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

Filter on List - null elements - very interesting

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Say i have one-to-many relationship between Product and ProductNames. So Product has a List of ProductNames.


Actually we are using List for productNames.and for list, in order to maintain the sequence, it stores the sequence in
extra seperate column in DB.
Now say, after applying the filer conditions, 0th, 2nd and 5th element (out of 10 )of the list gets selected..
Returned list size will of size 6 with 1st,3rd and 4th element as null and 0th,2nd and 5th element properly populated.
I guess this is done in hibernate for maitaining ordering of List.
What should i do if i don't want these null elements in the returned List?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'd actually use the Criteria API, although not everyone is as in love with it as I am.

I'd use the example class, and the excludeNone() method. It can easily be switched on and off, so your code can easily change depending on the type or results you're interested in. The Restrictions class can do something similar:

Helpful Methods of the Example Class

Sometimes, your database will contain a variety of entries that have been initialized to zero by default, even though a field initialized to zero is pretty much meaningless. By default, the Example class will match on zero valued properties, but you can reverse this behavior by invoking the excludeZeroes() method on the Example instance:



On the flipside, criteria queries ignore matching null values. However, you may actually want to pull records where a given property has been initialized to a null value. For example, you might want to retrieve every record where the loginName wasn't initialized properly, and has been assigned a null value. To enable matches on null values, you just call the excludeNone() method on your Example instance. The excludeNone() method has the added functionality of enabling matching on fields initialized to zero as well.



Using the Hibernate Criteria API

-Cameron McKenzie
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is something similar availbale in HQL?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic