我已经尝试了很多线程中提到的东西。但似乎没有一个能解决我的问题。请告诉我哪里出错了。
楷模
@Entity
public class MySession {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(unique=true)
private String sessionId;
@OneToMany(mappedBy="session")
private List<MyRequest> requestList;
}
@Entity
public class MyRequest{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
private MySession session;
}
在我的 servlet 中:
SessionHome sh = new SessionHome(); //DAO Class
MyRequestHome mrh = new MyRequestHome(); //DAO Class
MySession session = sh.findBySessionId(sessionId); //Object is in the detached state
session.setUpdated(new Date()); //Update the Session update time stamp
session = sh.merge(session); //Update the database with the session. I was hoping merge would reattach the detached object
MyRequest mr = new MyRequest(hsrequest, newSessionToken);
mr.setSession(session);
mrh.persist(mr); //PersistentObjectException here. This is because of the MySession object I have set over here.
首先,我从数据库中获取 MySession 对象,对其进行更新并将其设置在新创建的 MyRequest 对象中。当我尝试持久化 MyRequest 对象时,它给了我 PersistentObjectException。如果需要任何其他信息,请告诉我。