2014-08-28 72 views
0

我有一個POJO,這是非常簡單的只有一個ID,我得到一個異常,當我刪除一個記錄,保存和更新工作正常。如果我不使用commit(),它不會發出異常,但是如果我使用它,就會給出。 這裏是代碼...休眠刪除拋出使用tx.commit()的異常

public class DeleteCompanyRecords { 
public static boolean deleteCompany(String comname){ 
    Boolean res=false; 
    Session session = null; 
    String comid=GenCompanyCodeWhenGivenCompanyName.genCompanyCode(comname); 
    System.out.println(comid); 
    Transaction tx = null; 
    try{ 
     session = SessionCreate.getSessionFactory().openSession(); 
     tx=(Transaction) session.getTransaction(); 
     ElitSysCompanyMaster employee = 
       (ElitSysCompanyMaster)session.get(ElitSysCompanyMaster.class, comid); 
     session.delete(employee);res=true; 
     session.flush();//tx.commit() also gives exception here. 
    }catch (Exception e) { 
     session.getTransaction().rollback(); 
     e.printStackTrace(); 
     res=false; 

    } finally { 
     if (session != null) 
      if (session.isOpen()){ 
       session.close();} 
    } 
    return res; 
} 

在上面的代碼,如果我使用tx.commit也會引發相同的異常以及中頻(wasCommitted())語句。請指導。我是唯一一個爲這個完整項目工作的人。沒有其他人。

該表格是一張非常簡單的表格,至今沒有任何映射。 異常是如此。

SEVERE: Servlet.service() for servlet [jsp] in context with path [/SecurePass] threw exception [org.hibernate.TransactionException: Transaction not successfully started] with root cause 
org.hibernate.TransactionException: Transaction not successfully started 
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:202) 
    at classes.deleteclasses.DeleteCompanyRecords.deleteCompany(DeleteCompanyRecords.java:25) 
    at org.apache.jsp.DeleteRecords.DeleteCompany_jsp._jspService(DeleteCompany_jsp.java:81) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Unknown Source) 

回答

1

您需要begin()交易,所以session.getTransaction().begin()

請注意:您引用的異常不會出現在commit()flush()中,而是出自catch塊的rollback()。你可以在堆棧跟蹤中看到。

+0

OOOPS ..!我怎麼能忘記那個..!謝謝 – Abhijeet 2014-08-28 07:45:10