Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: what is better initialize a list in entity as null or empty list [closed]

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
like image 402
usertest Avatar asked May 31 '26 08:05

usertest


1 Answers

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

like image 69
Henning Luther Avatar answered Jun 02 '26 21:06

Henning Luther



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!