don't flush the Session after an exception occurs異常

來源:互聯網
上載者:User

最近在用hibernate的時候遇到了這個異常,去了一些論壇看了看,一些博主為了賺人氣不公布答案,這苦了一群小鳥們,今天我同樣遇到這個異常,並且我也解決了,不知道大家報錯的地方是不是一樣?



我的源碼:


package cn.ecgonline.eis.business.workstation;import java.util.Calendar;import javax.annotation.Resource;import org.apache.log4j.Logger;import cn.ecgonline.eis.business.INewWorkstationProcessor;import cn.ecgonline.eis.business.IRecordLogProcessor;import cn.ecgonline.eis.business.core.ActionCode;import cn.ecgonline.eis.dao.IWorkstationDao;import cn.ecgonline.eis.err.ErrorCode;import cn.ecgonline.eis.model.WorkstationModel;import cn.ecgonline.eis.model.LogModel;import cn.ecgonline.eis.model.WorkstationModel.EnAble;/** * 添加裝置資訊業務實作類別 * @author 陳文龍 * @date 2013-9-11 下午2:23:22 */public class NewWorkstationProcessorImpl implements INewWorkstationProcessor{    /**     * 日誌操作類,主要是寫入動作記錄     */    private static Logger logger = Logger.getLogger(NewWorkstationProcessorImpl.class);                                                                                                                                                                                                                                                                                   /**     * 日誌的持久化操作類,主要是將資料動作記錄寫入資料庫     */    @Resource(name = "RecordLogProcessor")    private IRecordLogProcessor m_procRecordLog;                                                                                                                                                                                                                                                                                   /**     * 工作站資訊持久化操作引用     */    @Resource(name = "WorkstationDao")    private IWorkstationDao m_daoWorkstation;                                                                                                                                                                                                                                                                                   /**     * 添加工作站資訊業務入口方法     * @param workstationModel 工作站資訊     * @return 返回錯誤碼     */    @Override    public int execute(WorkstationModel workstationModel)    {        int errorCode = ErrorCode.ErrSystem;                                                                                                                                                                                                                                                                                           if(m_daoWorkstation != null)        {            try            {                workstationModel.setEnables(EnAble.Enable);                m_daoWorkstation.save(workstationModel);            }            catch (Exception e)            {                errorCode = ErrorCode.ErrExcption;                logger.error(e);                return errorCode;            }            errorCode = ErrorCode.ErrOk;                                                                                                                                                                                                                                                                                               }else        {            logger.error("IWorkstationDao is null");        }        // 記錄日誌        LogModel lm = new LogModel();        lm.setActioncode(ActionCode.Act_NewDevice);        lm.setErrorcode(errorCode);        lm.setWorkstation(workstationModel);        lm.setCreatetime(Calendar.getInstance().getTime());        if (null != m_procRecordLog)            m_procRecordLog.execute(lm, true);        else            logger.error("null record log processor");        return errorCode;    }}




NewWorkstationProcessorImpl類是個業務類,我是把一個model類WorkstationModel持久化到資料庫後,再把這個類的添加到LogModel日誌類裡面WorkstationModel類和logModel類是一對多關聯的),再把日誌類持久化,我用了spring mvc ,聲明式事務也配置了,資料庫也是自動認可的,本來以為沒什麼問題,但是還是出錯了






異常:



圖一:


嚴重: Servlet.service() for servlet [MVC] in context with path [/JLEIS] threw exceptionorg.hibernate.AssertionFailure: null id in cn.ecgonline.eis.model.LogModel entry (don't flush the Session after an exception occurs)    at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:79)    at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:194)    at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:156)    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:225)    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)    at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:480)    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)    at $Proxy20.onApplicationEvent(Unknown Source)    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:326)    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:919)    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)    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.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:119)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)    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:472)    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)    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:1023)    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)    at java.lang.Thread.run(Unknown Source)



圖二:


2013/09/17 16:21:56  WARN SqlExceptionHelper:143 - SQL Error: 207, SQLState: 42S222013/09/17 16:21:56 ERROR SqlExceptionHelper:144 - 列名 'systemparam_id' 無效。2013/09/17 16:21:56  WARN SqlExceptionHelper:143 - SQL Error: 207, SQLState: 42S222013/09/17 16:21:56 ERROR SqlExceptionHelper:144 - 列名 'workstation_id' 無效。




圖一是報的異常,圖二是控制台列印的sql問題,這是因為找不到資料庫表中的systemparam_id欄位,所以當持久化LogModel類的時候,你添加其他關聯類別到LogModel裡面的時候,它是按照關聯類別的id來找的,找不到id就無法關聯對象。





解決辦法:


修改前:

650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/21251J923-0.jpg" style="float:none;" title="0F4~2L8I8WK25U3_`$]UR{E.jpg " alt="165010864.jpg" />


修改後:


650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/21251J4O-1.jpg" style="float:none;" title=")B$0~Y@Y5`TGJS1OJ(O8A]R.jpg " alt="165008297.jpg" />

把systemparamid改為systemparam_id就行了,你們的話可能是其他欄位,按著改就是了。


建議大家報錯,多看控制台列印的sql資訊,它有可能能協助你找到突破口。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.