gpt4 book ai didi

hibernate - 如何防止出现 org.hibernate.loader.MultipleBagFetchException

转载 作者:行者123 更新时间:2023-12-04 01:58:38 25 4
gpt4 key购买 nike

我得到以下异常:

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

我使用的bean是:

@Entity
@Table(name="user")
public class User implements Serializable {

/**
*
*/
private static final long serialVersionUID = -5689970869179555442L;

@Id
@Column(name="username")
/*@NotNull*/
private String username;

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

@Column(name="email")
/*@Email*/
private String email;

@LazyCollection(LazyCollectionOption.FALSE)
@ManyToMany
@JoinTable(name="user_follower",joinColumns={@JoinColumn(name="username")},
inverseJoinColumns={@JoinColumn(name="follower")})
private List<User> followers = new ArrayList<User>(0);

/*@LazyCollection(LazyCollectionOption.FALSE)*/
@OneToMany(fetch=FetchType.EAGER)
@JoinTable(name="user_message",joinColumns={@JoinColumn(name="username")},
inverseJoinColumns={@JoinColumn(name="message_date")})
@OrderBy(value="messageDate")
private List<Message> messages = new ArrayList<Message>(0);

public User() {

}

在dao中我有以下代码:

public void followUser(String userToFollow, String username) {
System.out.println("repository invoked");
session=sessionFactory.getCurrentSession();
User user=retrieveUser(username);
@SuppressWarnings("unchecked")
List<User> followers=session.createCriteria(User.class).setFetchMode("followers", FetchMode.JOIN)
.add(Restrictions.eq("username", username)).list();
for(User follower: followers){
if(follower.getUsername().equals(userToFollow)){
//do nothing
}else{
followers.add(retrieveUser(userToFollow));
user.setUserFollowers(followers);
}
}
session.saveOrUpdate(user);
}

我不知道该怎么做才能防止这种异常

最佳答案

我有类似的问题,我通过将 List 更改为 Set 来解决它。我认为 Hibernate 不需要关心 ORDER BY(只是猜测)。

关于hibernate - 如何防止出现 org.hibernate.loader.MultipleBagFetchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6909796/

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