'Hibernate save empty set twice throws JpaSystemException

I have a class managed by Hibernate (version 5.6.7.Final), for the sake of brevity, with these two fields

@Entity(name = "Announcement")
@Table(name = "ANNOUNCEMENT")
@Data
@EqualsAndHashCode
@SuperBuilder
public class Announcement {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;    /** Unique identifier **/

    @OneToMany(mappedBy = "announcement", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private Set<AnnouncementLocation> announcementLocations = new HashSet<>();

}

I can add all I wish to the set, save, remove, save again, remove, add, save and all updates in the database are correct and no exceptions thrown.

However, if I leave the list empty, save, then save again I get:

org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.foo.bar.model.Announcement.announcementLocations; nested exception is org.hibernate.HibernateException

I've battled and got nowhere with this puzzling behaviour. Please assist! Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source