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

发布于 2024-08-22 13:22:34 字数 3663 浏览 6 评论 0 原文

当我尝试运行使用 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 不是很精通,而且我已经束手无策,所以任何帮助将不胜感激。

I'm getting NoPrimaryKeyException when I try to run one of my unit tests which uses DBUnit. The datatable is created using Hibernate and is a join table between two classes mapping a many to many relationship. The annotations that define the relationship are as follows:

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

The other entries in in the XML file I'm using to define the dataset seem to work fine but not the join table. I get the following exception:

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)

the Dataset entry in question looks like this:

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

And matches up with the database which has both fields as primary keys (The Databse is MS SQL Server if that helps) There are corresponding entries in the two tables being Joined defined in the following 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>

Do I have to define the primary keys in the hibernate annotations? If so How do I do so? do I have to change the way I define my dataset to imply that the two columns are a joint primary key?

I'm not very proficient with hibernate or DBUnit for that matter and I'm at my wits end so any assistance would be really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

a√萤火虫的光℡ 2024-08-29 13:22:34

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

是的,你知道,这是 Hibernate/JPA 无法为你猜测的事情之一。为此,请使用 @Id 注释

Do I have to define the primary keys in the hibernate annotations? If so, how do I do so?

Yes, you do, this is one of the things Hibernate/JPA can't guess for you. To do so, annotate the property holding the identifier (or the getter for this property) with the @Id annotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文