Hibernate does not ignore empty composite collection elements
Description
After fixing HHH-7610, null and empty composite/embedded values are supposed to be considered equivalent. This isn't true when it comes to collection elements.
When a map, list, bag, or idbag collection element is null, Hibernate does not insert the null value into the collection table. After is fixed, the same will be true for {set}} collection elements.
Currently, if a collection element is an empty composite value, Hibernate will insert the row containing the foreign key and null for each element column.
Later, when the collection element is read, Hibernate's behavior depends on how hibernate.create_empty_composites.enabled is set.
If hibernate.create_empty_composites.enabled=false, then Hibernate ignores the null element; i.e., it does not be included in the initialized collection. Later, a ConstraintViolationException can be thrown in some cases.
Some examples:
if a new empty composite value is added to a Set, then Hibernate will attempt to add another row with null element columns, violating the unique constraint on the Set element columns;
if the entity is deleted with an empty collection, but there is still a row with null element columns in the collection table, the a foreign key constraint will be violated because Hibernate does not delete collections from the collection table if the collection is empty.
If hibernate.create_empty_composites.enabled=true, then Hibernate will read the null element and inject an empty composite element into the collection.
The fix for this issue will fix these issues so that a collection element that is an empty composite value is treated the same way as a null value:
empty composite collection elements will not be inserted into the collection table;
null composite elements read from the database will be ignored and will not be included in the initialized collection.
After fixing HHH-7610,
null
and empty composite/embedded values are supposed to be considered equivalent. This isn't true when it comes to collection elements.When a
map
,list
,bag
, oridbag
collection element isnull
, Hibernate does not insert thenull
value into the collection table. After is fixed, the same will be true for {set}} collection elements.Currently, if a collection element is an empty composite value, Hibernate will insert the row containing the foreign key and
null
for each element column.Later, when the collection element is read, Hibernate's behavior depends on how
hibernate.create_empty_composites.enabled
is set.If
hibernate.create_empty_composites.enabled=false
, then Hibernate ignores thenull
element; i.e., it does not be included in the initialized collection. Later, aConstraintViolationException
can be thrown in some cases.Some examples:
if a new empty composite value is added to a
Set
, then Hibernate will attempt to add another row withnull
element columns, violating the unique constraint on theSet
element columns;if the entity is deleted with an empty collection, but there is still a row with null element columns in the collection table, the a foreign key constraint will be violated because Hibernate does not delete collections from the collection table if the collection is empty.
If
hibernate.create_empty_composites.enabled=true
, then Hibernate will read thenull
element and inject an empty composite element into the collection.The fix for this issue will fix these issues so that a collection element that is an empty composite value is treated the same way as a null value:
empty composite collection elements will not be inserted into the collection table;
null
composite elements read from the database will be ignored and will not be included in the initialized collection.