I continent-country-city entities (all different tables). I map both collections (Continent:
List<Country> and Country:
List<City>) as this:
Code:
@OneToMany(mappedBy="child", orphanRemoval=true, cascade=CascadeType.ALL)
@Valid
@NotNull
@Size(min=1)
Lets say
continent has 1
country and that country has 1
city in DB. I recreate exactly the same structure using '
new' keyword (with all ids set as if it was loaded from db). So I have 1 existing record on each level. Then I add to the collection of
continent.countries new Country(new City) with some data set. When I try to call
session.merge(continent) I'm getting a
NotNull constraint error saying property cities can not be null. I think it complains about that newly created
Country and its
City. But If I remove that
@NotNull constraint of the
List<City> it works ok and populates a collection of
List<City> cities with 1 record. So why does it complain at all?
In short:
session.merge(continent) updates existing
countries and its
cities. It also creates a newly added country and its newly added city. But it happens only when used
without @NotNull on cities collection.