当我尝试使用@OneToMany映射保存对象时,我遇到了ConstraintViolationException。 Hibernate似乎认为我正在尝试将NULL放入映射列表中,或者它可能将其放在那里。这个问题似乎偶尔会发生:有些数据库很容易重现,但是对于其他数据库,我根本无法让它发生。
我在Hibernate和Hibernate Annotations都是新手,所以任何帮助都会非常感激。
我有两个类:Camera和AlarmZone(扩展了第3类,Zone)。除其他外,Camera包含AlarmZone对象的ID列表。这是类声明:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "cameras")
public class Camera
{
private Serializable id;
private List<Serializable> alarm_zones = new ArrayList<Serializable>();
...
@Id(generate = GeneratorType.SEQUENCE)
@Type(type = "java.lang.Long")
public java.io.Serializable getId()
{
return this.id;
}
public void setId(java.io.Serializable id)
{
this.id = id;
}
@OneToMany(targetEntity = java.lang.Long.class, fetch = FetchType.EAGER)
@JoinTable(table = @Table(name = "cameraalarmzones"))
public List<Serializable> getAlarmZones()
{
return alarm_zones;
}
public void setAlarmZones(List<Serializable> alarm_zones)
{
this.alarm_zones = alarm_zones;
}
...
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "zones")
public class Zone
{
private Serializable id;
...
@Id(generate = GeneratorType.SEQUENCE)
@Type(type = "java.lang.Long")
public java.io.Serializable getId()
{
return this.id;
}
public void setId(java.io.Serializable id)
{
this.id = id;
}
}
@Entity
@Table(name = "alarmzones")
public class AlarmZone extends Zone
{
...
}
每当我保存一个对象时,我都会使用我所理解的样板提交代码:
public Persistable makePersistent(Object entity)
{
try
{
Hibernate.getSession().beginTransaction();
Hibernate.getSession().saveOrUpdate(entity);
Hibernate.getSession().flush();
Hibernate.getSession().getTransaction().commit();
} catch (RuntimeException e)
{
Hibernate.getSession().getTransaction().rollback();
logger.error("makePersistent(" + entity.getClass().getSimpleName()
+ ")", e);
}
return entity;
}
当我保存AlarmZone时会出现问题,然后将其ID放入相机列表并尝试保存相机。为了获得更好的调试信息,我遵循https://forum.hibernate.org/viewtopic.php?p=2220295&sid=a7ccc3be32137436c8062a99c8372a9e的建议并将我的批量大小设置为0并在log4j中打开DEBUG级别调试。这是错误的堆栈跟踪(以及前面几行):
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractCollectionPersister.java:947) - Deleting collection: [x.y.z.Camera.alarmZones#641]
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractBatcher.java:311) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractBatcher.java:346) - delete from cameraalarmzones where cameras_id=?
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractBatcher.java:424) - preparing statement
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (NullableType.java:79) - binding '641' to parameter: 1
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractCollectionPersister.java:977) - done deleting collection
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractCollectionPersister.java:999) - Inserting collection: [x.y.z.Camera.alarmZones#641]
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractBatcher.java:319) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractBatcher.java:470) - closing statement
[2009-10-23 14:33:07,606] DEBUG [http-8443-Processor25] (AbstractBatcher.java:311) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
[2009-10-23 14:33:07,622] DEBUG [http-8443-Processor25] (AbstractBatcher.java:346) - insert into cameraalarmzones (cameras_id, elt) values (?, ?)
[2009-10-23 14:33:07,622] DEBUG [http-8443-Processor25] (AbstractBatcher.java:424) - preparing statement
[2009-10-23 14:33:07,622] DEBUG [http-8443-Processor25] (NullableType.java:79) - binding '641' to parameter: 1
[2009-10-23 14:33:07,622] DEBUG [http-8443-Processor25] (NullableType.java:79) - binding '649' to parameter: 2
[2009-10-23 14:33:07,622] DEBUG [http-8443-Processor25] (AbstractBatcher.java:319) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
[2009-10-23 14:33:07,622] DEBUG [http-8443-Processor25] (AbstractBatcher.java:470) - closing statement
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (JDBCExceptionReporter.java:63) - could not insert collection: [x.y.z.Camera.alarmZones#641] [insert into cameraalarmzones (cameras_id, elt) values (?, ?)]
java.sql.SQLException: Attempt to insert null into a non-nullable column: column: ALARMZONES table: CAMERAALARMZONES in statement [insert into cameraalarmzones (cameras_id, elt) values (?, ?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1039)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
at sun.reflect.GeneratedMethodAccessor56.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:292)
at $Proxy25.flush(Unknown Source)
at com.silkspeed.ssparts.server.PersistableDAOImpl.makePersistent(PersistableDAOImpl.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:68)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy38.makePersistent(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:179)
at org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:33)
at org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:76)
at org.springframework.remoting.support.RemoteInvocationBasedExporter.invokeAndCreateResult(RemoteInvocationBasedExporter.java:112)
at org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.handleRequest(HttpInvokerServiceExporter.java:80)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:392)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:357)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at x.y.z.server.SessionInfo.doFilter(SessionInfo.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
[2009-10-23 14:33:07,637] WARN [http-8443-Processor25] (JDBCExceptionReporter.java:71) - SQL Error: -10, SQLState: 23000
[2009-10-23 14:33:07,637] ERROR [http-8443-Processor25] (JDBCExceptionReporter.java:72) - Attempt to insert null into a non-nullable column: column: ALARMZONES table: CAMERAALARMZONES in statement [insert into cameraalarmzones (cameras_id, elt) values (?, ?)]
[2009-10-23 14:33:07,637] ERROR [http-8443-Processor25] (AbstractFlushingEventListener.java:299) - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert collection: [x.y.z.Camera.alarmZones#641]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1058)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
at sun.reflect.GeneratedMethodAccessor56.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:292)
at $Proxy25.flush(Unknown Source)
at com.silkspeed.ssparts.server.PersistableDAOImpl.makePersistent(PersistableDAOImpl.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:68)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy38.makePersistent(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:179)
at org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:33)
at org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:76)
at org.springframework.remoting.support.RemoteInvocationBasedExporter.invokeAndCreateResult(RemoteInvocationBasedExporter.java:112)
at org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.handleRequest(HttpInvokerServiceExporter.java:80)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:392)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:357)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at x.y.z.server.SessionInfo.doFilter(SessionInfo.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: Attempt to insert null into a non-nullable column: column: ALARMZONES table: CAMERAALARMZONES in statement [insert into cameraalarmzones (cameras_id, elt) values (?, ?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1039)
... 58 more
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (JDBCTransaction.java:152) - rollback
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (JDBCTransaction.java:193) - re-enabling autocommit
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (JDBCTransaction.java:163) - rolled back JDBC Connection
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (JDBCContext.java:207) - after transaction completion
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (ConnectionManager.java:296) - aggressively releasing JDBC connection
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (ConnectionManager.java:333) - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (DriverManagerConnectionProvider.java:129) - returning connection to pool, pool size: 1
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (SessionImpl.java:434) - after transaction completion
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (SessionImpl.java:368) - automatically closing session
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (SessionImpl.java:296) - closing session
[2009-10-23 14:33:07,637] DEBUG [http-8443-Processor25] (ConnectionManager.java:267) - connection already null in cleanup : no action
[2009-10-23 14:33:07,637] ERROR [http-8443-Processor25] (PersistableDAOImpl.java:87) - makePersistent(Camera)
org.hibernate.exception.ConstraintViolationException: could not insert collection: [x.y.z.Camera.alarmZones#641]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1058)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:980)
at sun.reflect.GeneratedMethodAccessor56.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:292)
at $Proxy25.flush(Unknown Source)
at com.silkspeed.ssparts.server.PersistableDAOImpl.makePersistent(PersistableDAOImpl.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:68)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy38.makePersistent(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:179)
at org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:33)
at org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:76)
at org.springframework.remoting.support.RemoteInvocationBasedExporter.invokeAndCreateResult(RemoteInvocationBasedExporter.java:112)
at org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.handleRequest(HttpInvokerServiceExporter.java:80)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:392)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:357)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at x.y.z.server.SessionInfo.doFilter(SessionInfo.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: Attempt to insert null into a non-nullable column: column: ALARMZONES table: CAMERAALARMZONES in statement [insert into cameraalarmzones (cameras_id, elt) values (?, ?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1039)
... 58 more
这是我的hibernate配置文件:
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:file:dbfilename;shutdown=true</property>
<property name="connection.username">*************</property>
<property name="connection.password">*************</property>
<property name="connection.autocommit">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.jdbc.fetch_size">0</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="com.silkspeed.sightlogix.cs.AlarmZone"/>
<mapping class="com.silkspeed.sightlogix.cs.Camera" />
<mapping class="com.silkspeed.sightlogix.cs.Zone"/>
...
提前致谢。
答案 0 :(得分:3)
您的代码存在一些问题:
long
(或Long
)。为什么?Zone
(或AlarmZone
(如果需要)实例,而不是ids。除非关系真的是ManyToMany,否则不需要中间表,在这种情况下应该如此映射(单个AlarmZone是否属于多个Camera?如果是,那就是多对多)。Camera
实体上有继承注释。为什么?它是另一个层次结构的一部分吗?样本映射:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "cameras")
public class Camera {
private long id;
private List<AlarmZone> alarm_zones = new ArrayList<AlarmZone>();
@SequenceGenerator(name="Camera_Gen", sequenceName="cameras_seq")
@Id @GeneratedValue(generator="Camera_Gen")
public long getId() {
return this.id;
}
@OneToMany(mappedBy="camera") // this maps a bi-directional relationship
public List<AlarmZone> getAlarmZones() {
return alarm_zones;
}
...
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "zones")
public class Zone {
// same deal as above
...
}
@Entity
@Table(name = "alarmzones")
public class AlarmZone extends Zone {
private Camera camera;
@ManyToOne
public Camera getCamera() {
return this.camera;
}
}