Java 如何在hibernate中为多个关系在现有集合中添加条目?
我有如下User.java类Java 如何在hibernate中为多个关系在现有集合中添加条目?,java,mysql,hibernate,spring-mvc,Java,Mysql,Hibernate,Spring Mvc,我有如下User.java类 @Entity @Table (name="users") public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="userId") private int userId; @Column(name="userName") @NaturalId p
@Entity
@Table (name="users")
public class User implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="userId")
private int userId;
@Column(name="userName")
@NaturalId
private String userName;
@JsonProperty( value = "password", access = JsonProperty.Access.WRITE_ONLY)
@Column(name="password")
private String password;
@Column(name="firstName")
private String firstName;
@Column(name="lastName")
private String lastName;
@Enumerated(EnumType.STRING)
private UserRole userRole;
@JsonIgnore
@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name="user_followings",
joinColumns=@JoinColumn(name="userId"),
inverseJoinColumns=@JoinColumn(name="followingId"))
private Set<User> followings = new HashSet<>();
@JsonIgnore
@ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="followings")
private Set<User> followers = new HashSet<>();
// getters and setters
@Override
public int hashCode() {
return Objects.hash(userName);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
return Objects.equals(userName, other.getUserName());
}
}
@实体
@表(name=“users”)
公共类用户实现可序列化{
@Id@GeneratedValue(策略=GenerationType.IDENTITY)
@列(name=“userId”)
私有int用户id;
@列(name=“userName”)
@归化
私有字符串用户名;
@JsonProperty(value=“password”,access=JsonProperty.access.WRITE_ONLY)
@列(name=“password”)
私有字符串密码;
@列(name=“firstName”)
私有字符串名;
@列(name=“lastName”)
私有字符串lastName;
@枚举(EnumType.STRING)
私有用户角色;
@杰索尼奥雷
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(name=“user\u followers”,
joinColumns=@JoinColumn(name=“userId”),
inverseJoinColumns=@JoinColumn(name=“followId”))
私有集follows=新HashSet();
@杰索尼奥雷
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy=“followers”)
private Set followers=new HashSet();
//接球手和接球手
@凌驾
公共int hashCode(){
返回Objects.hash(用户名);
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
用户其他=(用户)对象;
返回Objects.equals(userName,other.getUserName());
}
}
我想在用户想要添加其他用户时为用户添加以下内容。我第一次尝试为用户添加以下内容时,它被添加到以下列表中,但之后,它抛出一个错误,指出“具有相同标识符值的不同对象已与会话关联:[com.me.pojo.user#2]”。我的跟进方法如下。我错过了什么
public void follow(User user, String followingId) throws UserException {
try {
begin();
User followingUser = getUserFromId(followingId);
if (followingUser.equals(user)) {
throw new HibernateException("Same User");
}
Set<User> followings = user.getFollowings();
if(!followings.contains(followingUser)) {
followings.add(followingUser);
user.setFollowings(followings);
getSession().update(user);
}
commit();
close();
} catch (HibernateException e) {
rollback();
throw new UserException(e.getMessage());
}
}
public void follow(用户、字符串followind)抛出UserException{
试一试{
begin();
User followuser=getUserFromId(followId);
如果(以下用户等于(用户)){
抛出新的HibernateException(“同一用户”);
}
设置follows=user.getfollows();
如果(!followers.contains(followeruser)){
followers.add(followeruser);
用户。设置以下项(以下项);
getSession().update(用户);
}
提交();
close();
}捕获(休眠异常e){
回滚();
抛出新的UserException(例如getMessage());
}
}
无需调用update()。用户是一个托管实体。在事务结束时,您所做的每一个更改都是透明的、自动保存的。如果我不调用UpDATE(),那么它不会被持久化到MySQLThen,您的会话管理和/或事务管理是错误的。对于会话和事务管理,我需要考虑什么?好吗?我已经解决了问题..作为参数获取的用户对象属于不同的会话。我的会话管理出现了一些问题。谢谢你的指导。