We're updating the issue view to help you get more done. 

ElementCollection referencing other primivive attribute breaks column nullability meta

Description

The subsequent entity has to attributes "parent" and "children":

  • both are of primitive type.

  • "children" is an element collection referencing "parent"

  • "parent" is nullable

However, the subsequent example below does not work anymore in Hibernate 5.0.4 and 5.1.0. It used to work fine in 4.3. For some reason, it expects parent to be not-nullable. Checking the metadata (org.hibernate.mapping.Property) shows that value is of Type SimpleValue and in there is a column "parent" that is not nullable anymore. At some point a nullable "parent" column is created, but it seems to get overriden by the element table of "children".

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 import java.util.HashSet; import java.util.Set; import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Entity @Access(AccessType.FIELD) @Table(name = "PCY_M_PARENT_CHILD_CD") public class ParentChildCd { @Id @Column(name = "CODE", nullable = false, length = 50) @Size(max = 50) private String code; public String getCode() { return code; } public void setCode(String __value) { // NOSONAR this.code = __value; } @Column(name = "ACTIVE", nullable = false) @NotNull private boolean active; public boolean isActive() { return active; } public void setActive(boolean __value) { // NOSONAR this.active = __value; } @Column(name = "PARENT", nullable = true) private String parent; public String getParent() { return parent; } public void setParent(String __value) { this.parent = __value; } @Column(name = "code", nullable = true) @ElementCollection(fetch = FetchType.EAGER) @CollectionTable(name = "PCY_M_PARENT_CHILD_CD", joinColumns = { @JoinColumn(name = "PARENT", referencedColumnName = "code") }) private Set<String> children = new HashSet<String>(); public Set<String> getChildren() { return children; } public void setChildren(Set<String> __value) { this.children = __value; } public boolean equals(Object obj) { return obj == this || (obj instanceof ParentChildCd && getCode() != null && getCode().equals(((ParentChildCd) obj).getCode())); } public int hashCode() { if (getCode() == null) return super.hashCode(); return getCode().hashCode(); } }
1 2 3 4 5 6 7 8 @Test public void testParentChild() { ParentChildCd m = new ParentChildCd(); m.setCode("a"); m.setParent(null); em.persist(m); em.flush(); }

Environment

None

Status

Assignee

Unassigned

Reporter

Remo Meier

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.1.0

Priority

Critical