Hibernate 中使用了meger()保存一次正常,第二次一直等着
刚开始在hibernate保存一个一对多的关系时用save();结果Illegal attempt to associate a collection with two open sessions;然后用meger(),但是用了发现第一次正常保存进数据库,但是第二次执行的时候一直停在那个页面不动了。
DAO:
public boolean saveRes(Reservation res){
Transaction tx = null;
Session session = null;
//System.out.println("Doctorname:"+res.getDoctorname()+"username" +res.getUsername());
try {
session = getSession();
tx = session.beginTransaction();
session.merge(res);
tx.commit();
return true;
} catch (Exception e) {
if(tx != null) tx.rollback();
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return false;
}
Action:
public String saveRes(){
HttpServletRequest request=ServletActionContext.getRequest();
int docid=Integer.parseInt(request.getParameter("docid"));
String userid=request.getParameter("userid");
String time=request.getParameter("res_time");
String dtime=request.getParameter("date");
String res_time=time+" "+dtime;
User u=iservice.findUserById(userid);
Doctor d=iservice.findDoctorById(docid);
System.out.println("u:"+userid+"d"+docid+"time:"+res_time);
Reservation reservation=new Reservation();
reservation.setRes_time(res_time);
reservation.setDoctor(d);
reservation.setUser(u);
if(iservice.saveRes(reservation)){
return SUCCESS;
}
vo类:
public class Reservation {
private Integer res_id;
private String res_time;
private Integer res_num;
private Integer res_password;
private String res_status;
private User user;
private Doctor doctor;
省略setget
}