I'm using Hibernate and have the following JPA model:
@Entity
public class MyEntity {
@Embedded
MyEmbeddable embedded;
}
@Embeddable
public class MyEmbeddable {
@Column
private String name;
@ElementCollection
@Enumerated(EnumType.STRING)
@Size(min = 1)
private Set<MyEnum> usage;
@NotNull
private LocalDate localDate;
}
MyEntity can contain MyEmbeddable, but the whole embeddable should be optional. So I would prefer that MyEntity.embedded is null when all properties of MyEmbeddable are empty or null.
However, when I'm reading such an entity from DB, Hibernate initializes embedded as non-null - which leads to constraint errors when the entity gets flushed to the database again.
As a workaround I would have to write some code to null the embedded property before flushing/persisting - which seems like a very bad idea to me.
I suspect that the ElementCollection causes this inizialitation behaviour - because according to JPA spec ElementCollections are always initialized empty (not null).
Any ideas/hint how you can tell Hibernate to not initialized embedded at all?