我目前正在尝试使用Spring Data存储库来删除我的某些实体。删除调用没有任何异常/错误消息,但实体之后并未被删除。
这些是我的实体:
public class Board implements Serializable {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)")
private UUID uuid;
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval = true, mappedBy = "board")
private List<Post> posts = new ArrayList<Post>();
}
和
public class Post implements Serializable {
@Id
@GeneratedValue
private long id;
@ManyToOne(optional = false)
@JoinColumn(name="board_uuid", updatable = false, nullable = false)
@JsonBackReference
private Board board;
}
这个存储库尽可能简单:
@Repository
public interface PostRepository extends CrudRepository<Post, Long> {
}
delete调用类似于
postRepository.delete(50);
你有什么想法,为什么这个更改没有反映在数据库中?
编辑1:
我找到了一种解决方法,但我还是不明白真正的问题所在。 如果我像这样删除帖子,它就“有效”了(由于约束违规,有几个例外情况,但帖子仍然被删除):
post.setBoard(null);
postRepo.delete(post);
编辑2:
当我查看执行的SQL语句时,我发现Hibernate甚至没有尝试删除。唯一发生的事情是这两个select语句:
Hibernate: select post0_.id as id1_1_0_, post0_.board_uuid as board_uu6_1_0_, post0_.content as content2_1_0_, post0_.x as x3_1_0_, post0_.y as y4_1_0_, post0_.z as z5_1_0_, board1_.uuid as uuid1_0_1_ from Post post0_ left outer join Board board1_ on post0_.board_uuid=board1_.uuid where post0_.id=?
Hibernate: select posts0_.board_uuid as board_uu6_0_0_, posts0_.id as id1_1_0_, posts0_.id as id1_1_1_, posts0_.board_uuid as board_uu6_1_1_, posts0_.content as content2_1_1_, posts0_.x as x3_1_1_, posts0_.y as y4_1_1_, posts0_.z as z5_1_1_ from Post posts0_ where posts0_.board_uuid=?
编辑 3
结果发现在帖子上的cascade=CascadeType.ALL似乎是问题所在。如果没有它,删除就能正常工作了(但是现在我错过了对帖子更改的级联)。