本文介绍了Hibernate的@ManyToMany删除关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体:用户和UsersList

I have 2 entities: User and 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;
    }
}

和code像这样的:

// 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中我有3行中的用户(USER_ID)表。

In step 1 I have 3 rows in the USERS (USER_ID) table.

在步骤2中(第二个评论)我在USERS_LIST(USERS_LIST_ID)表和插入连接表USERS_LIST_USERS(USER_ID,USERS_LIST_ID)3行1列。

After step 2 (second comment) I have 1 row in the USERS_LIST (USERS_LIST_ID) table and into the join table USERS_LIST_USERS (USER_ID, USERS_LIST_ID) 3 rows.

我想在第3步实现如下:当我从USERS表中删除一个用户 - 让我们说有USER_ID = 4的用户,我想只是一行USER_ID = 4从连接表被删除,和其他人保持

What I want to achieve in step 3 is the following: when I delete one user from the table USERS - let's say user with USER_ID = 4, I want just the row with USER_ID = 4 from the join table to be deleted, and the others to remain.

是否有标注解决我的问题?

Is there an annotation solution to my problem?

推荐答案

您需要的是@JoinTable属性:

What you need is the @JoinTable attribute:

Hibernate不知道,很多一对多的关系互相引用,并可能会创建两个连接表。如果指定的关系,双方在同一@JoinTable它会正常工作。

Hibernate does not know that the many-to-many relations refer to each other and will probably create two join tables. If you specify the same @JoinTable on both sides of the relation it will work as expected.

请确保joinColumn和inverseJoinColumn是对立的对立
关系的两侧。

Make sure that the joinColumn and the inverseJoinColumn are opposites on the opposingsides of the relation.


  • 在一边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;
    }

}

这篇关于Hibernate的@ManyToMany删除关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:08