问题描述
我有 2 个实体:User 和 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;
}
}
和这样的代码:
// 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 行.
In step 1 I have 3 rows in the USERS (USER_ID) table.
在第 2 步(第二条评论)之后,我在 USERS_LIST (USERS_LIST_ID) 表中有 1 行,在连接表 USERS_LIST_USERS (USER_ID, USERS_LIST_ID) 中有 3 行.
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 删除关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!