春天 + JPA多对多关系
我对 Spring 很陌生,我正在努力使多对多关系按我的预期工作。关系工作正常,表已创建并且数据已正确插入。我期望的是,当我清空列表时(即我从“组”类型的对象中清空 ArrayList“用户”)时,我希望系统从数据库中删除关系 - 但事实并非如此。
我有以下课程:
@Entity
@Table(name = "`group`")
public class Group
{
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(
name = "`user_has_group`",
joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id")
)
private List<User> users = new ArrayList<User>();
...
}
@Entity
@Table(name = "`user`")
public class User
{
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@ManyToMany(mappedBy = "users")
private List<Group> groups = new ArrayList<Group>();
...
}
这是 DAO:
@Repository
public class GroupJpaDao implements GroupDao
{
private EntityManager em;
@Transactional
public void save(Group group)
{
this.em.merge(group);
}
...
@PersistenceContext
void setEntityManager(EntityManager entityManager)
{
this.em = entityManager;
}
}
@Repository
public class UserJpaDao implements UserDao
{
private EntityManager em;
@Transactional
public void save(User user)
{
this.em.merge(user);
}
...
@PersistenceContext
void setEntityManager(EntityManager entityManager)
{
this.em = entityManager;
}
}
这是测试方法:
@Test
public void test()
{
Group g = new Group();
g.setName("Test group");
groupDao.save(g); // Works fine - inserts the group into the database
User u = new User();
u.setName("Test user");
userDao.save(u); // Works fine - inserts the user into the database
g.addUser(u);
groupDao.save(g); // Works fine - adds the many-to-many relationship into the database
g.removeAllUsers();
groupDao.save(g); // Doesn't work - I'm expecting it to remove all the related relationships from the database but it doesn't!
}
我做错了什么或者是不可能做的事情吗?
任何提示都非常感激。
谢谢你!
I'm very new to Spring and I'm trying to make a many-to-many relationship work as I expect. The relationships works fine, the tables are created and the data is inserted correctly. What I expect, is that when I empty a List (i.e. I empty the ArrayList "users" from an object of type "Group"), I expect the system to remove the relationships from the database - but it doesn't.
I have the following classes:
@Entity
@Table(name = "`group`")
public class Group
{
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(
name = "`user_has_group`",
joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id")
)
private List<User> users = new ArrayList<User>();
...
}
@Entity
@Table(name = "`user`")
public class User
{
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@ManyToMany(mappedBy = "users")
private List<Group> groups = new ArrayList<Group>();
...
}
Here are the DAOs:
@Repository
public class GroupJpaDao implements GroupDao
{
private EntityManager em;
@Transactional
public void save(Group group)
{
this.em.merge(group);
}
...
@PersistenceContext
void setEntityManager(EntityManager entityManager)
{
this.em = entityManager;
}
}
@Repository
public class UserJpaDao implements UserDao
{
private EntityManager em;
@Transactional
public void save(User user)
{
this.em.merge(user);
}
...
@PersistenceContext
void setEntityManager(EntityManager entityManager)
{
this.em = entityManager;
}
}
Here is the test method:
@Test
public void test()
{
Group g = new Group();
g.setName("Test group");
groupDao.save(g); // Works fine - inserts the group into the database
User u = new User();
u.setName("Test user");
userDao.save(u); // Works fine - inserts the user into the database
g.addUser(u);
groupDao.save(g); // Works fine - adds the many-to-many relationship into the database
g.removeAllUsers();
groupDao.save(g); // Doesn't work - I'm expecting it to remove all the related relationships from the database but it doesn't!
}
Am I doing something wrong or is it something not possible to do?
Any hint is really appreciated.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我重读了你的问题,现在答案很明确了。您创建一个组
g
,并保存它。但由于您的保存方法使用merge
,并且您没有考虑merge
返回的值来将其分配给g
,因此您保留合并瞬态组 g,该组从未分配任何 ID。因此,每次调用 merge 时,您实际上都会创建一个新组,而不是修改之前创建的组。将保存方法更改为
并始终将结果重新分配给
g
:当然,必须为用户执行相同的操作。
I reread your question, and now the answer is clear. Your create a Group
g
, and save it. But since your save method usesmerge
, and you don't take into account the value returned bymerge
to assign it tog
, you keep merging the transient group g, which never has any ID assigned. So, each time merge is called, you actually create a new group rather than modifying the previously created one.Change the save method to
and always reassign the result to
g
:Of course, the same must be done for the user.
您只是从组中删除用户,这还不够。您需要从该组中删除所有用户,并从所有这些用户的组列表中删除该组。
you're only removing the users from the group, that's not enough. you need to remove all the users from the group and remove that group from all those users' list of groups.