Session.refresh() causes infinite loop in bi-directional one-to-many using CascadeType.ALL

Description

Prerequisites:

  • Two entities, A and B, have a many-to-many relationship

  • The link table has additional data, and is thus modeled as its own entity, C

  • C has a composite primary key which defines @ManyToOne relationships to A and B

  • A has a @OneToMany mapping to C using mappedBy. This @OneToMany mapping also defines cascade = CascadeType.ALL.

When using Session.refresh() on A, Hibernate gets stuck on an infinite loop, which eventually results in a StackOverflowError. A minimalistic test case that reproduces this problem is attached. The problem was first noticed with version 4.3.5.Final, but is also reproducible with 4.3.11.Final.

Workaround: Do not cascade the refresh operation. E.g. replace CascadeType.ALL with {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}

Environment

All environments

Assignee

Unassigned

Reporter

Markus Yrjölä

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure