NoPrimaryKeyException from DBUnit when loading a dataset into a database

依然范特西╮ 提交于 2020-01-13 14:43:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2318037/noprimarykeyexception-from-dbunit-when-loading-a-dataset-into-a-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!