我需要一个实验者的观点。 我想做的是一个投票系统。我有一份需求清单,你可以投票:ok或ko。
如果该人遇到ok / ko,我会向我的servlet发送一个ajax查询,该查询检查该人是否已经对此请求进行了投票,否则会保存投票。
在我的servlet中:
int id = Integer.parseInt(request.getParameter("id"));
Demande demande = daoDemande.get(id);
User user = (User)request.getSession().getAttribute("membre");
PrintWriter out = response.getWriter();
if(!demande.hasVoted(user)){
Vote vote = new Vote();
vote.setDemande(demande);
vote.setAuteur(user);
if(request.getParameter("vote").equals("Pour"))
vote.setVote(true);
else
vote.setVote(false);
daoVote.save(vote);
demande.setVote(vote);
daoDemande.save(demande);
out.println( "<h4>Votre vote a été pris en copmte!</h4>" );
}
else
out.println( "<h4>Vous avez déjà voté!</h4>" );
}
当该人在同一需求上再次投票时,我得到了上述例外情况。 谁能告诉我问题在哪里?
daoVote.save方法:
@Override
public void save(Vote vote) throws Exception {
Session session = DAOHBM.getInstance().connect();
if(vote.getId() == -1)
session.save(vote);
else
session.update(vote);
DAOHBM.getInstance().close(session);
}
DAOHBM.connect:
public Session connect() throws Exception {
if (sessionFactory == null)
sessionFactory = new Configuration().configure().buildSessionFactory();//null le prof ma darhe ev sa solution!!
Class.forName("com.mysql.jdbc.Driver");
Session session = sessionFactory.openSession();
session.beginTransaction();
return session;
}
DAOHBM.close:
public void close(Session session) throws Exception {
session.getTransaction().commit();
session.close();
}
例外:
févr. 11, 2014 1:44:33 PM org.apache.struts.action.RequestProcessor processException
Avertissement: Unhandled Exception thrown: class org.hibernate.LazyInitializationException
févr. 11, 2014 1:44:33 PM org.apache.catalina.core.StandardWrapperValve invoke
Grave: Servlet.service() for servlet [action] in context with path [/Cahier_de_charge] threw exception [org.hibernate.LazyInitializationException: could not initialize proxy - no Session] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at metier.User_$$_javassist_0.equals(User_$$_javassist_0.java)
at metier.Demande.hasVoted(Demande.java:46)
at action.ActionVoteUneDemande.execute(ActionVoteUneDemande.java:37)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)