开发者

Hibernate ConstraintViolationException in join table

开发者 https://www.devze.com 2022-12-09 13:30 出处:网络
I\'m running into a ConstraintViolationException when I try to save an object with a @OneToMany map in it. Hibernate either seems to think that I\'m trying to put a NULL into the mapped list or it may

I'm running into a ConstraintViolationException when I try to save an object with a @OneToMany map in it. Hibernate either seems to think that I'm trying to put a NULL into the mapped list or it may be putting it there itself. The problem seems to happen sporadically: with some databases it's easily reproducible, but with others I can't get it to happen at all.

I'm kind of a newbie at both Hibernate and Hibernate Annotations, so any help at all would be much appreciated.

I have two classes: Camera and AlarmZone (which extends a 3rd class, Zone). Among other things, Camera contains a list of IDs of AlarmZone objects. Here's the class declarations:

@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
{
    ...
}

Whenever I save an object, I use what I understand to be boilerplate commit code:

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;
}

The problem happens when I save an AlarmZone, then put its ID into the Camera's list and try to save the camera. To get better debug info, I followed the advice from https://forum.hibernate.org/viewtopic.php?p=2220295&sid=a7ccc3be32137436c8062a99c8372a9e and set my batch size to be 0 and turned on DEBUG level debugging in log4j. Here's the stack trace for the error (along with the preceding few lines):

[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

Here's my hibernate config file:

  <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"/>

...

Thanks in advance.


There are several problems with your code:

  1. You have ids declared as Serializable instead of plain long (or Long). Why?
  2. You should map actual Zone (or AlarmZone if you want) instances in your OneToMany relationship instead of ids. There's no need for intermediate table unless the relationship is really ManyToMany in which case it should be mapped as such (can a single AlarmZone belong to more than one Camera? If so, it's many-to-many).
  3. You have inheritance annotation on your Camera entity. Why? Is it part of another hierarchy?
  4. The "boilerplate" code you're using is bad. Transaction should enclose an entire unit of work, not individual entity. Explicit flushing is unnecessary.

Sample mapping:

@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;
  }
}

Details on identifier mapping and association mapping.

0

精彩评论

暂无评论...
验证码 换一张
取 消