Hello,
The detailed issue description, the test that reproduces the issue could be found at the next pull request
Input - the below mapping.
@Entity(name = "Post") @Table(name = "post") public static class Post { @Id @GeneratedValue private Long id; private String title; public Post() {} public Post(String title) { this.title = title; } @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) @OrderColumn(name = "entry") private List<PostComment> comments = new ArrayList<>(); public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<PostComment> getComments() { return comments; } } @Entity(name = "PostComment") @Table(name = "post_comment") public static class PostComment { @Id @GeneratedValue private Long id; private String review; public PostComment() {} public PostComment(String review) { this.review = review; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getReview() { return review; } public void setReview(String review) { this.review = review; } @Override public String toString() { return "PostComment{" + "id=" + id + ", review='" + review + '\'' + '}'; } }
The test
@Test public void testLifecycle() { doInJPA(entityManager -> { Post post = new Post("First post"); post.getComments().add(new PostComment("My first review")); post.getComments().add(new PostComment("My second review")); post.getComments().add(new PostComment("My third review")); entityManager.persist(post); entityManager.flush(); LOGGER.info("Remove head"); PostComment deletedComment = post.getComments().remove(0); LOGGER.info("Deleted {}", deletedComment); }); }
The test result
insert into post_comment (review, id) values ('My first review', 2) insert into post_comment (review, id) values ('My second review', 3) insert into post_comment (review, id) values ('My third review', 4) insert into post_post_comment (Post_id, entry, comments_id) values (1, 0, 2) insert into post_post_comment (Post_id, entry, comments_id) values (1, 1, 3) insert into post_post_comment (Post_id, entry, comments_id) values (1, 2, 4) - Remove head - Deleted PostComment{id=2, review='My first review'} delete from post_post_comment where Post_id=1 and entry=2 update post_post_comment set comments_id=3 where Post_id=1 and entry=0 SQL Error: -104, SQLState: 23505 integrity constraint violation: unique constraint or index violation; UK_GB03LQUKC5JC1U847L17M311Q table: POST_POST_COMMENT