This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Filter on List - null elements - very interesting  RSS feed

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?
author and cow tipper
Posts: 4968
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
Boost this thread!