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?
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
posted 11 years ago
Is something similar availbale in HQL?
Do not threaten THIS beaver! Not even with this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!