JPA @ElementCollection [List <string>]删除错误,使用play Framework 1.2.7 </string>

时间:2013-12-24 10:22:26

标签: jpa

我想删除一个模型对象,该对象有一个字符串集合列表。我正在使用Play Framework 1.2.7。

类结构如下:

@Entity
@Table(name = "DATASET_CORE")
@NamedQueries(value = {
@NamedQuery(name = "datasetcore.delete", query = "DELETE FROM DataSetCore dataCore WHERE dataCore.id = :dataCoreId")
})
public class DataSetCore extends Model {
 :
 @Required(message = "dataset.keyword.required")
 @Column(nullable = false,name = "KEYWORD")
 @ElementCollection(fetch = FetchType.LAZY)
 public List<String> keywords;
 :
}

我的删除代码如下:

Query q = Publish.em().createNamedQuery("datasetcore.delete");
q.setParameter("dataCoreId", dataSet.id);
q.executeUpdate();

它出现以下错误:

15:33:30,053 WARN  ~ SQL Error: 23503, SQLState: 23503
15:33:30,053 ERROR ~ Referential integrity constraint violation: "FK9B153AF2E51FF179: PUBLIC.DATASETCORE_KEYWORDS FOREIGN KEY(DATASETCORE_ID) REFERENCES PUBLIC.DATASET_CORE(ID) (2)"; SQL sta
tement:
delete from DATASET_CORE where id=? [23503-166]

 ERROR.....

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute update query
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1389)
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1317)
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1399)
        at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:108)
        at com.hbase.metadata.services.DataSetCoreService.delete(DataSetCoreService.java:39)
        at com.hbase.metadata.services.MetaDataCoreService.delete(MetaDataCoreService.java:241)
        at com.hbase.ref.MetaDataCoreTestCase.deleteMetaData(MetaDataCoreTestCase.java:61)
        at com.hbase.ref.MetaDataCoreTestCase.executeTest(MetaDataCoreTestCase.java:39)
        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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:114)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:58)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:24)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
        at play.test.TestEngine.run(TestEngine.java:112)
        at controllers.TestRunner$1.doJobWithResult(TestRunner.java:71)
        at controllers.TestRunner$1.doJobWithResult(TestRunner.java:1)
        at play.jobs.Job.call(Job.java:146)
        at play.jobs.Job$1.call(Job.java:66)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:207)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
        at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute update query
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:110)
        at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:423)
        at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:283)
        at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1288)
        at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:117)
        at org.hibernate.ejb.QueryImpl.internalExecuteUpdate(QueryImpl.java:188)
        at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:99)
        ... 46 more
Caused by: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK9B153AF2E51FF179: PUBLIC.DATASETCORE_KEYWORDS FOREIGN KEY(DATASETCORE_ID) REFERENCES PUBLIC.DATASET_CO
RE(ID) (2)"; SQL statement:
delete from DATASET_CORE where id=? [23503-166]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
        at org.h2.message.DbException.get(DbException.java:169)
        at org.h2.message.DbException.get(DbException.java:146)
        at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:414)
        at org.h2.constraint.ConstraintReferential.checkRowRefTable(ConstraintReferential.java:431)
        at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:307)
        at org.h2.table.Table.fireConstraints(Table.java:862)
        at org.h2.table.Table.fireAfterRow(Table.java:879)
        at org.h2.command.dml.Delete.update(Delete.java:99)
        at org.h2.command.CommandContainer.update(CommandContainer.java:73)
        at org.h2.command.Command.executeUpdate(Command.java:226)
        at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:143)
        at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:129)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
        at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:101)
        ... 52 more

我使用MySQL作为数据库。在数据库中,它创建了一个连接表。生成的表的脚本是:

CREATE TABLE `datasetcore_keywords` (
  `DataSetCore_id` bigint(20) NOT NULL,
  `KEYWORD` varchar(255) NOT NULL,
  KEY `FK9B153AF2E51FF179` (`DataSetCore_id`),
  CONSTRAINT `FK9B153AF2E51FF179` FOREIGN KEY (`DataSetCore_id`) REFERENCES `dataset_core` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

任何人都可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

这是从您的错误中复制的:

PUBLIC.DATASETCORE_KEYWORDS FOREIGN KEY(DATASETCORE_ID)
  REFERENCES PUBLIC.DATASET_CORE(ID)

它显示您要删除的行是从名为 DATASETCORE_KEYWORDS 的另一个表 DATASETCORE_ID 引用的。您必须先从表中删除该行,然后才能执行当前的删除操作。