Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

Filter on List - null elements - very interesting

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: 5000
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?
Do not threaten THIS beaver! Not even with this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic