Hibernate @ManyToMany删除关系

时间:2009-08-19 11:54:21

标签: hibernate annotations many-to-many

我有2个实体:User和UsersList。

@Entity
@Table(name = "USERS")
public class User {
    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @ManyToMany(cascade = CascadeType.REMOVE, mappedBy = "users")
    private List<UsersList> usersLists = new ArrayList<UsersList>();

    public List<UsersList> getUsersLists() {
        return usersLists;
    }

    public void setUsersLists(List<UsersList> usersLists) {
        this.usersLists = usersLists;
    }
}

@Entity
@Table(name = "USERS_LIST")
public class UsersList {
    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @ManyToMany
    private List<User> users = new ArrayList<User>();

 public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }
}

和像这样的代码:

// 1
List<User> dbUsers; // 3 the user instances are persisted in DB
UsersList usersList = new UsersList();
usersList.setUsers(dbUsers);

// 2 - now persist the usersList using hibernate...
saveWithHibernate(usersList); 

//3 - delete using a persisted user
deleteWithHibernate(dbUsers.get(0).getId());

其中

deleteWithHibernate(Long id) {
        User usr = hibernateTemplate.get(User.class, id);
        hibernateTemplate.delete(usr);
}

在步骤1中,我在USERS(USER_ID)表中有3行。

在步骤2(第二个注释)之后,我在USERS_LIST(USERS_LIST_ID)表中有1行,并且在连接表USERS_LIST_USERS(USER_ID,USERS_LIST_ID)中有3行。

我想在步骤3中实现的目标如下:当我从表USERS中删除一个用户时 - 假设用户具有USER_ID = 4,我只想删除连接表中USER_ID = 4的行,和其他人留下来。

我的问题是否有注释解决方案?

2 个答案:

答案 0 :(得分:9)

您需要的是@JoinTable属性:

Hibernate不知道多对多关系是相互引用的,并且可能会创建两个连接表。如果在关系的两边指定相同的@JoinTable,它将按预期工作。

确保joinColumn和inverseJoinColumn在对立面上是对立的 关系的两面。

  • 一边是joinColumn =另一边是inverseJoinColumn
  • 一边是inverseJoinColumn =另一边是joinColumn

我希望这会有所帮助。

@Entity
public class Role extends Identifiable {

    @ManyToMany(cascade ={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinTable(name="Role_Permission",
            joinColumns=@JoinColumn(name="Role_id"),
            inverseJoinColumns=@JoinColumn(name="Permission_id")
        )
    public List<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }
}

@Entity
public class Permission extends Identifiable {

    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinTable(name="Role_Permission",
            joinColumns=@JoinColumn(name="Permission_id"),
            inverseJoinColumns=@JoinColumn(name="Role_id")
        )
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

}

答案 1 :(得分:-1)

我认为你必须作为这个注释

    @OnDelete(action = OnDeleteAction.CASCADE)
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

并将cascade.remove更改为CascadeType.ALL 你的代码必须是这样的

   @ManyToMany(cascade = CascadeType.ALL, mappedBy = "users")
   @OnDelete(action = OnDeleteAction.CASCADE)
   @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
   private List<UsersList> usersLists = new ArrayList<UsersList>();