Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate Spring数据积垢@OneToMany单向上的正向级联删除_Hibernate_Jpa - Fatal编程技术网

Hibernate Spring数据积垢@OneToMany单向上的正向级联删除

Hibernate Spring数据积垢@OneToMany单向上的正向级联删除,hibernate,jpa,Hibernate,Jpa,晚上好 我有一个带有以下签名的字段: @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true) @JoinColumn(name="username") private Collection<UserAuthority> authorities; 这是正确的吗 我能让它正确级联删除的唯一方法是,如果我有一个单独的UserAuthority存储库,并执行如下操作 ... authRe

晚上好

我有一个带有以下签名的字段:

@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="username")
private Collection<UserAuthority> authorities; 
这是正确的吗

我能让它正确级联删除的唯一方法是,如果我有一个单独的UserAuthority存储库,并执行如下操作

...
authRepo.delete(authorities);
...
任何帮助都会很棒


谢谢

如果将其映射为双向关系,则上面给出的代码将起作用:

 @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="user")   
 private Collection<UserAuthority> authorities;
MappedBy告诉Hibernate UserAuthority是所有者实体,即它具有定义关联的外键。因此,当您想要保存一个新的UserAssociation实体时,您需要确保设置引用的用户


这还将导致更优化的保存过程,因为Hibernate可以使用正确的外键插入UserAuthority记录,而不是执行插入然后更新来设置它

有趣的是,在Spring中我几乎完成了所有JDBC和纯sql的工作,Hibernate看起来非常棒,但与习惯于=)我有一个问题,如果我使用的是复合键,例如username/role是UserAuthority表的主键,我是否将@ManyToOne关系放在复合密钥上?您可以将@ManyToOne放在复合密钥中的属性上。这与UserAuthority类@ManyToOne@JoinColumn(name=“username”,insertable=false,updateable=false)@ForeignKey(name=“FK\u UA\u USER”)私有用户的以下添加一起工作:;
 @OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true, mappedBy="user")   
 private Collection<UserAuthority> authorities;
@ManyToOne
@JoinColumn(name="username")
private User user;