最近在用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 exception org.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: 42S22 2013/09/17 16:21:56 ERROR SqlExceptionHelper:144 - 列名 'systemparam_id' 无效。 2013/09/17 16:21:56 WARN SqlExceptionHelper:143 - SQL Error: 207, SQLState: 42S22 2013/09/17 16:21:56 ERROR SqlExceptionHelper:144 - 列名 'workstation_id' 无效。
图一是报的异常,图二是控制台打印的sql问题,这是因为找不到数据库表中的systemparam_id
字段,所以当持久化LogModel类的时候,你添加其他关联类到LogModel里面的时候,它是按照关联类的id来找的,找不到id就无法关联对象。
解决办法:
修改前:
修改后:
把systemparamid改为systemparam_id就行了,你们的话可能是其他字段,按着改就是了。
建议大家报错,多看控制台打印的sql信息,它有可能能帮助你找到突破口。