Spring Data Repository不会删除ManyToOne实体 [英] Spring Data Repository does not delete ManyToOne Entity

查看:99
本文介绍了Spring Data Repository不会删除ManyToOne实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring Data存储库来删除我的一些实体。删除调用没有任何异常/错误消息,但实体并未被删除。



这些是我的实体:

  public class Board实现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实现Serializable {

@Id
@GeneratedValue
private long id;

@ManyToOne(可选= FALSE)
@JoinColumn(name = board_uuid,可更新=假,可为空= FALSE)
@JsonBackReference
私人董事会板;
}

存储库非常简单:

  @Repository 
public interface PostRepository扩展CrudRepository< Post,Long> {
}

删除调用类似于

  postRepository.delete(50); 

任何想法为什么这个改变没有反映在数据库中?



编辑1:



我找到了解决方法,但我仍然不明白真正的问题。
如果我像这样删除帖子,那么它起作用(由于违反约束条件,有两个例外,但帖子被删除):

  post.setBoard(空); 
postRepo.delete(post);

编辑2:

当我查看执行的SQL语句时,我可以看到hibernate甚至没有尝试删除。出现这种情况的唯一事情是这两个选择语句:

 休眠:选择post0_.id如id1_1_0_,post0_.board_uuid如board_uu6_1_0_, post0_.content为content2_1_0_,post0_.x为x3_1_0_,post0_.y为y4_1_0_,post0_.z为z5_1_0_,board1_.uuid作为uuid1_0_1_从邮政post0_左外连接主板上board1_ = post0_.board_uuid其中board1_.uuid = post0_.id ? 
休眠:选择posts0_.board_uuid为board_uu6_0_0_,posts0_.id为id1_1_0_,posts0_.id为id1_1_1_,posts0_.board_uuid为board_uu6_1_1_,posts0_.content为content2_1_1_,posts0_.x为x3_1_1_,posts0_.y为y4_1_1_,posts0_ .z as z5_1_1_从帖子posts0_ where posts0_.board_uuid =?

编辑3
在帖子中发现级联= CascadeType.ALL似乎是问题所在。如果没有它,删除工作正常(但我现在缺少对帖子的更改级联)

解决方案

问题似乎是您正在使用 cascade = CascadeType.ALL ,其中还包括 CascadeType.PERSIST CascadeType.PERSIST 表示子实体完全由父级管理,您不能直接删除它。为了删除,您只需要将其从父项中删除即可。



您可以添加其他 CascadeTypes 所有。例如 CascadeType.REMOVE ,如果您想要的只是在删除父项时移除子项。


I'm am currently trying to use a Spring Data repository to delete some of my entities. The delete call works without any exceptions/error messages, but the entity is not deleted afterwards.

Those are my entities:

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>();
}

and

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;
}

The repository is as simple as it can be:

@Repository
public interface PostRepository extends CrudRepository<Post, Long> {
}

The delete call is something like

postRepository.delete(50);

Any ideas why this change doesn't reflect in the database?

Edit 1:

I found a workaround, but I still don't understand what the real problem is. It "works" if I delete the Post like this (there are a couple of exceptions because of constraint violations, but still the Post gets deleted):

post.setBoard(null);
postRepo.delete(post);

Edit 2:

When I have a look at the SQL statements executed I can see that hibernate is not even trying to delete. The only thing that happens are those two select statements:

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=?

Edit 3

Turns out the cascade=CascadeType.ALL on posts seems to be the problem. Without it the delete works fine (but I am missing the cascade of changes to posts now)

解决方案

The problem seems to be that you are using cascade=CascadeType.ALL, which also includes CascadeType.PERSIST. CascadeType.PERSIST means that the child entity is completely managed by the parent and you cannot delete it directly. In order to delete you just need to remove it from the parent.

You could just add the other CascadeTypes instead of all. e.g CascadeType.REMOVE, if the only thing you would want is to remove the child if the parent is removed.

这篇关于Spring Data Repository不会删除ManyToOne实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆