我得到以下异常:
我使用的bean是:org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包
@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);
}
我不知道如何防止此类异常
答案 0 :(得分:7)
我有类似的问题,我通过将List更改为Set来解决它。我认为Hibernate不需要关心ORDER BY(只是猜测)。