java Hibernate 中的级联类型保存更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34576803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cascade type save update in Hibernate
提问by underdog
I am using hibernate with JPA annotations for relationship mapping.
I have three entities in my code User
Group
& User_Group
我正在使用带有 JPA 注释的 hibernate 来进行关系映射。我的代码中有三个实体User
Group
&User_Group
User
& Group
are in a ManyToMany
relationship.
User
&Group
处于ManyToMany
关系中。
User_Group
is a kinda bridge table but with some additional fields. So here is the modified mapping code.
User_Group
是一个有点像桥接表,但有一些额外的字段。所以这里是修改后的映射代码。
User
用户
@Entity
@Table(name = "USERS")
public class User {
@OneToMany(mappedBy = "user")
private Set<UserGroup> userGroups
}
Group
团体
@Entity
@Table(name = "GROUPS")
public class Group {
@OneToMany(mappedBy = "group")
private Set<UserGroup> userGroups
}
UserGroup
用户组
@Entity
@Table(name = "USERS_GROUPS")
public class UserGroup {
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "USER_ID")
private User user;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "GROUP_ID")
private Group group;
}
When I set the user & group object to the usergroup & save it.
当我将用户和组对象设置为用户组并保存时。
User user = new User("tommy", "ymmot", "[email protected]");
Group group = new Group("Coders");
UserGroup userGroup = new UserGroup();
userGroup.setGroup(group);
userGroup.setUser(user);
userGroup.setActivated(true);
userGroup.setRegisteredDate(new Date());
session.save(userGroup);
Things work fine. With CascadeType.ALL
the group object & user object are updated too. But when I delete the userGroup object. The child object are deleted too.
一切正常。随着CascadeType.ALL
组对象和用户对象也更新。但是当我删除 userGroup 对象时。子对象也被删除。
Deletion of child objects is a strict no no.
删除子对象是严格禁止的。
There is no CascadeType.SAVE-UPDATE
in JPA, which just does save or update but no delete. How do I achieve this.
CascadeType.SAVE-UPDATE
JPA中没有,它只保存或更新但不删除。我如何实现这一目标。
If I remove the CascadeType.ALL
from the mapping the child objects don't get updated & I need them to be updated.
如果我CascadeType.ALL
从映射中删除子对象,则不会更新并且我需要更新它们。
回答by JB Nizet
SAVE_UPDATE is for save(), update(), and saveOrUpdate(), which are 3 Hibernate-proprietary methods. JPA only has persist()
and merge()
. So, if you want to use cascading on Hibernate-proprietary methods, you'll need to use Hibernate-proprietary annotations. In this case, Cascade.
SAVE_UPDATE 用于 save()、update() 和 saveOrUpdate(),它们是 3 个 Hibernate 专有方法。JPA 只有persist()
和merge()
。因此,如果您想在 Hibernate 专有方法上使用级联,则需要使用 Hibernate 专有注释。在这种情况下,级联。
Or you could stop using the Hibernate Session, and use the standard JPA API instead.
或者您可以停止使用 Hibernate Session,而使用标准的 JPA API。
回答by Bevor
Cascade.ALL contains DELETE too. I guess the solution is to use all important Cascade types you need except DELETE, so:
Cascade.ALL 也包含 DELETE。我想解决方案是使用除 DELETE 之外的所有重要级联类型,因此:
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE}))
in your UserGroup definitions.
在您的用户组定义中。