将数据集加载到数据库时,DBUnit的NoPrimaryKeyException

时间:2010-02-23 12:27:58

标签: java hibernate orm dbunit

当我尝试运行一个使用DBUnit的单元测试时,我收到NoPrimaryKeyException。数据表是使用Hibernate创建的,是两个映射多对多关系的类之间的连接表。定义关系的注释如下:

@Override
@ManyToMany
@JoinTable(name="offset_file_offset_entries", joinColumns={@JoinColumn(name="offset_entry_id")},inverseJoinColumns={@JoinColumn(name="file_description_id")})
public List<OffsetEntry> getOffsets() {

我用来定义数据集的XML文件中的其他条目似乎工作正常但不是连接表。我得到以下异常:

org.dbunit.dataset.NoPrimaryKeyException: offset_file_offset_entries
    at org.dbunit.operation.UpdateOperation.getOperationData(UpdateOperation.java:72)
    at org.dbunit.operation.RefreshOperation$UpdateRowOperation.<init>(RefreshOperation.java:266)
    at org.dbunit.operation.RefreshOperation.createUpdateOperation(RefreshOperation.java:142)
    at org.dbunit.operation.RefreshOperation.execute(RefreshOperation.java:100)
    at org.dbunit.ext.mssql.InsertIdentityOperation.execute(InsertIdentityOperation.java:217)
    at uk.co.sabio.obscheduler.application.dao.AbstractBaseDatabaseTest.setUp(AbstractBaseDatabaseTest.java:57)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runManaged(AbstractJUnit38SpringContextTests.java:332)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.access$0(AbstractJUnit38SpringContextTests.java:326)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests$1.run(AbstractJUnit38SpringContextTests.java:216)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runTest(AbstractJUnit38SpringContextTests.java:296)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runTestTimed(AbstractJUnit38SpringContextTests.java:253)
    at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runBare(AbstractJUnit38SpringContextTests.java:213)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    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)

有问题的数据集条目如下所示:

<offset_file_offset_entries offset_entry_id="1" file_description_id="1" />

并且与具有两个字段作为主键的数据库匹配(数据库是MS SQL Server,如果有帮助的话)在以下xml中定义的两个表中有相应的条目:

<dataset>
    <file_description file_path="src/test/resources/" file_pattern=".txt" file_description_id="1"/>
    <offset_file_description file_description_id="1"/>
    <offset_entries offset_entry_id="1" field_name="Field1" field_length="10" start_index="0"/>
    <offset_file_offset_entries offset_entry_id="1" file_description_id="1" />  
</dataset>

我是否必须在hibernate注释中定义主键?如果是这样我该怎么办?我是否必须更改我定义数据集的方式以暗示这两列是联合主键?

我对hibernate或DBUnit并不十分熟悉,而且我在我的智慧结束,所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:2)

  

我是否必须在hibernate注释中定义主键?如果是这样,我该怎么做?

是的,你知道,这是Hibernate / JPA无法猜到的事情之一。为此,请使用@Id annotation注释包含标识符(或此属性的getter)的属性。