In this example http://www.vogella.com/tutorials/JavaPersistenceAPI/article.html#jpaintro_entitymanager if an entity have a @OneToMany relationship. the List is initialized in the declaration like this:
@OneToMany(mappedBy = "family")
private final List<Person> members = new ArrayList<Person>();
I used to not initialize entities lists in my code, and always have to check if null. So My Question is: Is initializing lists (relationships) as an empty list in the declaration is what needs to be done normally or what ? and Why ?
What I'm currently doning
private final List<Person> members
In general "null" and "empty" have different meanings even if the behavior for jpa remains the same. Null means it was never initialized while empty means there are no entries. So if you need to handle those 2 cases different, you can do, if not it doesnt matter which way you go. For example null can mean, that you never load the data you need and you can load it after checking for null. While "empty" means you already load the data but there are no entries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With