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

Bidirectional @OneToMany does not support a non-nullable @OrderColumn

Description

If we define a bidirectional @OneToMany association with a non-nullable @OrderColumn:

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 @Entity(name = "ParentData") public static class ParentData { @Id long id; @OneToMany(mappedBy = "parentData", cascade = CascadeType.ALL, orphanRemoval = true) @OrderColumn(name = "listOrder", nullable = false) private List<ChildData> children = new ArrayList<>(); public long getId() { return id; } public void setId(long id) { this.id = id; } public List<ChildData> getChildren() { return children; } public void addChildData(ChildData childData) { childData.setParentData( this ); children.add( childData ); } } @Entity(name = "ChildData") public static class ChildData { @Id @GeneratedValue long id; @ManyToOne private ParentData parentData; public ChildData() { } public long getId() { return id; } public void setId(long id) { this.id = id; } public ParentData getParentData() { return parentData; } public void setParentData(ParentData parentData) { this.parentData = parentData; } }

The INSERT will fail since the order column is only updated after the INSERT:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 09:45:23,490 DEBUG SQL:94 - insert into ParentData (id) values (?) 09:45:23,495 TRACE BasicBinder:65 - binding parameter [1] as [BIGINT] - [1] 09:45:23,498 DEBUG SQL:94 - insert into ChildData (parentData_id, id) values (?, ?) 09:45:23,498 TRACE BasicBinder:65 - binding parameter [1] as [BIGINT] - [1] 09:45:23,498 TRACE BasicBinder:65 - binding parameter [2] as [BIGINT] - [1] 09:45:23,500 WARN SqlExceptionHelper:137 - SQL Error: 23502, SQLState: 23502 09:45:23,504 ERROR SqlExceptionHelper:142 - NULL not allowed for column "LISTORDER"; SQL statement: insert into ChildData (parentData_id, id) values (?, ?) [23502-196]

Environment

None

Status

Assignee

Unassigned

Reporter

Vlad Mihalcea

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.4.1

Priority

Major