[ACCEPTED]-Null vs empty collections in Hibernate-hibernate

Accepted answer
Score: 12

Have you tried to check in the getbList() method? You 5 could do:

if(bList.isEmpty()) 
    return null;
return bList;

Hibernate will always create an 4 object for your references, but you are 3 allowed to control the data inside of the 2 getter and setters. If the list has 0 elements 1 you can always return null.

Score: 2

I'm curious why you consider this a "limitation' - does 9 a null bList actually have a different meaning to your 8 application than an empty bList?

I think that 7 in most areas, a null collection and an 6 empty collection have the same semantic 5 meaning, which I would guess is why the 4 Hibernate developers sought to limit Hibernate 3 to only using one. Doesn't make much sense 2 to always check if (bList == null || bList.isEmpty) if the two always end up 1 meaning the same thing.

Score: 2

For handling in your code the obvious way 3 is in the getter, however that doesn't help 2 you if you want to evaluate it in HQL.

Two 1 ideas:

  • A constructor that sets it to NULL if empty.
  • A @PostLoad / @PostConstruct method that does the same.

More Related questions