简体   繁体   English

休眠:删除实体网络中的实体

[英]Hibernate: Delete an entity in network of entities

In my Spring boot app, there are two types of entities: User and Group : 在我的Spring Boot应用程序中,有两种类型的实体: UserGroup

  1. User can own 0 to N groups 用户可以拥有0到N个群组
  2. Group can have 1 to M members 群组可以有1到M个成员

In the User class there is a list of Group that he/she owns or is a member of, and in the Group class, there is a list of User (ie members). User类有列表Group ,他/她拥有或组织的成员,并在Group类,有名单User (即会员)。

These classes refer to each other using hibernate annotations. 这些类使用休眠注释相互引用。

class User {
    @ManyToMany(cascade = CascadeType.REFRESH)
    private List<Group> groups;
}

class Group {
    @ManyToOne(cascade = CascadeType.REFRESH)
    @NotNull
    @JoinColumn(name="OWNER_ID", referencedColumnName="id")
    private User owner;

    @ManyToMany
    @JoinTable(joinColumns = @JoinColumn(referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(referencedColumnName = "id"))
    private List<User> members;
}

In the User service layer there's a Delete method which is supposed to delete a user from the repository. User服务层中,有一个Delete方法,该方法应该从存储库中删除用户。 This delete should fire a series of actions: all the groups owned by that user gets deleted, and the deleted groups should be removed from list of groups of their members. 此删除操作应执行一系列操作:删除该用户拥有的所有组,并将删除的组从其成员的组列表中删除。 All these should be saved to the repository. 所有这些都应保存到存储库中。

If I add other types of entities to this network, this process gets much more complicated. 如果我将其他类型的实体添加到此网络,则此过程将变得更加复杂。

My question is: Doesn't hibernate handle this automatically ? 我的问题是:休眠不自动处理吗? Should I grab each member and delete the group one by one and save it to the repository ? 我应该抓住每个成员并逐个删除该组并将其保存到存储库中吗?

CascadeType.REFRESH means Managed objects can be reloaded from the database by using the refresh method. CascadeType.REFRESH表示可以通过使用refresh方法从数据库中重新加载托管对象。

This will not help you solving your requirement. 这不会帮助您解决要求。 You need to use “orphanRemoval = true” CascadeType. 您需要使用“ orphanRemoval = true” CascadeType。 “orphanRemoval = true” removes an owned object from the database when it's removed from its owning relationship. 当“ orphanRemoval = true”从数据库的拥有关系中删除时,该对象将从数据库中删除。

Example: 例:

EmployeeEntity.java EmployeeEntity.java

@Entity @Table(name = "Employee")
public class EmployeeEntity implements Serializable
{
    private static final long serialVersionUID = -1798070786993154676L;
    @Id @Column(name = "ID", unique = true, nullable = false)
    private Integer  employeeId;

    @Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
    private String  firstName;

    @Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
    private String lastName;

    @OneToMany(orphanRemoval = true, mappedBy = "employee")
    private Set<AccountEntity> accounts;

}

AccountEntity.java AccountEntity.java

@Entity (name = "Account") @Table(name = "Account")
public class AccountEntity implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id @Column(name = "ID", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer           accountId;

    @Column(name = "ACC_NO", unique = false, nullable = false, length = 100)
    private String            accountNumber;

    @ManyToOne
    private EmployeeEntity employee;
}   

OR You can use CascadeType.ALL too. 或者您也可以使用CascadeType.ALL。

For further reading, go through below link: 要进一步阅读,请通过以下链接:

CascadeTypes CascadeTypes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM