gpt4 book ai didi

java - Hibernate 中的多对多关系不返回结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:09 24 4
gpt4 key购买 nike

我有两个 beans 类——用户和用户组:

用户:

@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = -7482853955996650586L;

@Id
@Column(name = "id")
@GeneratedValue
private Integer id;

@Column(name = "login")
private String login;

@ManyToMany
@JoinTable(name = "user_x_userGroup",
joinColumns = {
@JoinColumn(
name = "user_id",
foreignKey = @ForeignKey(name = "FK_user_x_userGroup_user_id")
) },
inverseJoinColumns = {
@JoinColumn(
name = "userGroup_id",
foreignKey = @ForeignKey(name = "FK_user_x_userGroup_userGroup_id")
) })
private List<UserGroup> userGroups;

public User() {
this.userGroups = new ArrayList<>();
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getLogin() {
return this.login;
}

public void setLogin(String login) {
this.login = login;
}

public List<UserGroup> getUserGroups() {
return this.userGroups;
}

public void setUserGroups(List<UserGroup> userGroups) {
this.userGroups = userGroups;
}
}

用户组:

@Entity
@Table(name = "userGroup")
public class UserGroup implements Serializable, Comparable<UserGroup> {
private static final long serialVersionUID = -5924845694417474352L;

@Id
@Column(name = "id")
@GeneratedValue
private Integer id;

@Column(name = "title")
private String title;

@ManyToMany(mappedBy = "userGroups")
private List<User> users;

public UserGroup() {
this.users = new ArrayList<>();
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getTitle() {
return this.title;
}

public void setTitle(String title) {
this.title = title;
}

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

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

我在 UserServiceTest 中测试用户组列表的大小是否为 1:

public class UserServiceTest {

@BeforeClass
public static void prepareDatabase() throws DAOException {
MockDatabase.insertUsers();
MockDatabase.insertUserGroups();
}

@Test
public void shouldGetUserGroupSize() throws Exception {
UserService userService = new UserService();
User user = userService.find(1);
boolean condition = user.getUserGroups().size() == 1;
assertTrue("User groups size is incorrect!", condition);
}
}

不幸的是,它返回大小为 0。我可以在控制台中看到它向 user 和 userGroup 表中插入数据。

模拟数据库数据:

public static void insertUsers() throws DAOException {
UserService userService = new UserService();

User firstUser = new User();
firstUser.setLogin("first");
userService.save(firstUser);

User secondUser = new User();
secondUser.setLogin("second");
userService.save(secondUser);
}

public static void insertUserGroups() throws DAOException {
UserService userService = new UserService();
UserGroupService userGroupService = new UserGroupService();

UserGroup firstUserGroup = new UserGroup();
firstUserGroup.setTitle("Admin");
List<User> users = new ArrayList<>();
users.add(userService.find(1));
firstUserGroup.setUsers(users);
userGroupService.save(firstUserGroup);

UserGroup secondUserGroup = new UserGroup();
secondUserGroup.setTitle("Random");
userGroupService.save(secondUserGroup);
}

这里是查找方法:

public User find(Integer id) throws DAOException {
User result = (User) retrieveObject(User.class, id);
if (result == null) {
throw new DAOException("Object can not be found in database");
}
return result;
}

protected static Object retrieveObject(Class cls, Integer id) throws DAOException {
try {
Session session = Helper.getHibernateSession();
if (session == null) {
session = HibernateUtil.getSessionFactory().openSession();
Object o = session.get(cls, id);
session.close();
return o;
}
return session.get(cls, id);
} catch (HibernateException he) {
throw new DAOException(he);
}
}

在这里保存:

public User save(User user) throws DAOException {
storeObject(user);
return (User) retrieveObject(User.class, user.getId());
}

protected static void storeObject(Object object) throws DAOException {
try {
Session session = Helper.getHibernateSession();
session.saveOrUpdate(object);
session.flush();
session.beginTransaction().commit();
} catch (HibernateException he) {
rollback();
throw new DAOException(he);
}
}

它看起来将数据保存到 user 表和 userGroup 表,但它没有将数据保存到 user_x_userGroup 表。什么会导致这个问题?

最佳答案

您没有设置您在作为关系所有者的实体中持久保存的 UserGroup 实例。

UserGroup 中声明:

@ManyToMany(mappedBy = "userGroups")
private List<User> users;

这意味着双向关系的所有者端是 User 实体。

在您的实际代码中,所有者端 (User) 没有引用您要保留的 UserGroup 实体,因此 UserGroup实体将被保留,但它不会关联到您在 UserGroup 中设置的 users 字段。

所以适当设置一下应该就可以了:

UserGroup firstUserGroup = new UserGroup();
firstUserGroup.setTitle("Admin");
List<User> users = new ArrayList<>();
// change
User user = userService.find(1);
List<UserGroup> groups = new ArrayList<>();
groups.add(firstUserGroup);
user.setUserGroups(groups);
// end change
users.add(user);
firstUserGroup.setUsers(users);
userGroupService.save(firstUserGroup);
...

关于java - Hibernate 中的多对多关系不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42089312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com