Map of @Embeddables has values loaded only when != null
Activity
Former user August 16, 2017 at 9:57 PM
Rejecting because this is expected behavior.
Former user July 25, 2017 at 7:41 PM
Here is a comment that explains the rationale for not persisting a null collection element: https://hibernate.atlassian.net/browse/HHH-772?focusedCommentId=41974&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-41974
Former user July 22, 2017 at 9:09 PM
If you are adding an empty string as a collection element, then Hibernate should persist the empty string in the collection table. If your database automatically converts empty strings to null
, then you would end up with null
in your collection table, and Hibernate would end up ignoring the collection element when it is read.
Former user July 21, 2017 at 6:06 AMEdited
This is expected behavior. Hibernate should not be writing null
elements to the collection table. If Hibernate happens to read a null
element, it should be ignored.
I just fixed a bug (https://hibernate.atlassian.net/browse/HHH-11881#icft=HHH-11881) with Set
collections, where null
elements were (incorrectly) being persisted.
Currently, there is an inconsistency with empty composite values. I am in the process of fixing that (https://hibernate.atlassian.net/browse/HHH-11883#icft=HHH-11883).
This behavior does not depend on dialect. If Hibernate is adding a null
element to a collection table, then that is a bug.
Petar Tahchiev May 10, 2017 at 2:03 PM
It could be database-specific - under mysql the NULL values are stored. I haven't checked hsql though.
Hello,
here's my setup:
@Entity(name = "product") public class Product extends BaseProduct { @ElementCollection(targetClass = LocalizedValue.class) @CollectionTable(name = "product_description_lv", joinColumns = @JoinColumn(name = "product_id"), indexes = { @Index(name = "idx_" + "product_description_lv", columnList = "product_id") }, foreignKey = @ForeignKey(name = "fk_" + "product_description_lv")) @MapKeyColumn(name = "locale") private Map<Locale, LocalizedValue> description;
where
LocalizedValue
is:@Embeddable @Access(AccessType.FIELD) public class LocalizedValue { @Lob @Column(length = 50000, name = "val") private String value;
I create one
Product
assign two values for it's description (first entry is <Locale.ENGLISH, null> and the second is <Locale.GERMAN, "test">). Then I select the product from the database, and inspect thedescription
map - the result is it only has 1 element in the map - <Locale.GERMAN, "test"> one.I will create a test project soon.