0

当我使用@Id 在实体上定义字段时,我似乎遇到了麻烦,并且还有一个与字段名称不匹配的@Column 注释。IE

public class MyEntity {
    @Id
    @Column(name = "foo")
    private Long id;

   ...   
}

当我尝试使用存储库 save() 方法持久保存它时,我得到:

org.springframework.beans.factory.BeanCreationExeption
org.springframework.dao.DataIntegrityViolationException
Integrity constraint violation: 
    NOT Null check constraint; SYS_CT_10083 table: MyEntity column ID

如果 @Column 注释中的名称更改为“id”(以匹配字段名称和 getId() 访问器),则它可以完美运行。

这发生在一个测试中,我从我的实体创建了一个嵌入式 H2 或 HSQL 数据库(我都尝试过)。

通过挖掘日志,我可以看到该表的创建不正确:

[tool.hbm2ddl.SchemaUpdate] 
create table MyEntity (
    foo bigint not null, 
    ..., 
    id integer not null, 
    primary key (id))

因此,问题似乎在于存在 @Id 注释时如何生成表。

有人对我应该在哪里解决这个问题有任何想法吗?在我看来,spring data 或 hibernate 中可能存在错误,但我不太确定如何最好地解决这个问题。

仅供参考 - 我正在使用 Spring 3.2.4.RELEASE、spring-data-jpa 1.3.4.RELEASE 和 hibernate-entitymanager 4.0.1.Final

对于那些喜欢阅读堆栈跟踪的人,下面是一个完整的跟踪示例。

感谢任何人都可以提供的任何帮助!

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.foo.integration.repositories.JobRepositoryTest': Invocation of init method failed; nested exception is org.springframework.dao.DataIntegrityViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10083 table: JOB column: ID; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10083 table: JOB column: ID

            at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)

            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:396)

            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1475)

            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:388)

            at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:111)

            at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)

            at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)

            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)

            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)

            at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)

            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)

            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)

            at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

            at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

            at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

            at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

            at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

            at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

            at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)

            at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

            at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)

            at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

            at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: org.springframework.dao.DataIntegrityViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10083 table: JOB column: ID; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10083 table: JOB column: ID

            at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:643)

            at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:106)

            at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)

            at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)

            at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)

            at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)

            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

            at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92)

            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

            at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)

            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)

            at com.sun.proxy.$Proxy38.saveAndFlush(Unknown Source)

            at com.foo.integration.repositories.JobRepositoryTest.initialiseData(JobRepositoryTest.java:31)

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

            at java.lang.reflect.Method.invoke(Method.java:606)

            at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:344)

            at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:295)

            at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:130)

            ... 27 more

Caused by: org.hibernate.exception.ConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10083 table: JOB column: ID

            at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74)

            at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)

            at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)

            at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)

            at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)

            at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)

            at com.sun.proxy.$Proxy42.executeUpdate(Unknown Source)

            at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:56)

            at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2849)

            at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3290)

            at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:80)

            at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:272)

            at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:264)

            at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:186)

            at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)

            at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)

            at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1081)

            at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:973)

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

            at java.lang.reflect.Method.invoke(Method.java:606)

            at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)

            at com.sun.proxy.$Proxy36.flush(Unknown Source)

            at org.springframework.data.jpa.repository.support.SimpleJpaRepository.flush(SimpleJpaRepository.java:404)

            at org.springframework.data.jpa.repository.support.SimpleJpaRepository.saveAndFlush(SimpleJpaRepository.java:372)

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

            at java.lang.reflect.Method.invoke(Method.java:606)

            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:333)

            at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:318)

            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

            at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)

            at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)

            at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)

            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)

            at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)

            ... 42 more

Caused by: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10083 table: JOB column: ID

            at org.hsqldb.jdbc.Util.sqlException(Unknown Source)

            at org.hsqldb.jdbc.Util.sqlException(Unknown Source)

            at org.hsqldb.jdbc.JDBCPreparedStatement.fetchResult(Unknown Source)

            at org.hsqldb.jdbc.JDBCPreparedStatement.executeUpdate(Unknown Source)

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

            at java.lang.reflect.Method.invoke(Method.java:606)

            at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)

            ... 75 more

Caused by: org.hsqldb.HsqlException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10083 table: JOB column: ID

            at org.hsqldb.error.Error.error(Unknown Source)

            at org.hsqldb.Table.enforceRowConstraints(Unknown Source)

            at org.hsqldb.Table.insertSingleRow(Unknown Source)

            at org.hsqldb.StatementDML.insertSingleRow(Unknown Source)

            at org.hsqldb.StatementInsert.getResult(Unknown Source)

            at org.hsqldb.StatementDMQL.execute(Unknown Source)

            at org.hsqldb.Session.executeCompiledStatement(Unknown Source)

            at org.hsqldb.Session.execute(Unknown Source)

            ... 82 more
4

2 回答 2

1

在大大简化了应用程序之后,我确定这个异常是有 2 个实体引用同一个表的副作用,其中第二个表确实有一个名为“id”的字段。

IE

@Entity
@Table(name = "my_table")
public class MyEntity {
    @Id @Column(name = "pk") private Long id;
    ...
}

@Entity
@Table(name = "my_table")
public class MyOtherEntity {
    @Id @Column(name = "id") private Long id;
    ...
}

在我看来,这似乎导致 Hibernate/JPA 生成并验证由两个实体组合而成的表,其中(在上面的示例中)“pk”和“id”都是非空字段,并且(可能是因为按字母顺序较晚)主键设置为 MyOtherEntity 的@Id。

我不确定这是否是设计使然。当然,我可以看到有多个小实体引用一个巨大的平面表,这在使用“遗留”数据库时可能很有用。但这确实让我感到震惊。尤其是它处理 2 个单独的 @Id 字段(均非空,但只有一个主键)的方式。

于 2013-10-02T17:16:41.483 回答
0

通常使用生成器为 id 列创建值。

尝试:

@Entity
public class MyEntity {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "foo")
    private Long id;

   ...   
}
于 2013-10-02T10:15:32.970 回答