When writing a dbunit test for the CohortServiceTest, I encountered this non-informative error:
org.dbunit.dataset.NoPrimaryKeyException: COHORT_MEMBER
In the Cohort mapping file the cohort_member table is mapped like any hibernate collection:
<set name="memberIds" cascade="none" lazy="true" table="cohort_member">
<key column="cohort_id" />
<element column="patient_id" type="integer" />
</set>
It looks normal enough. Cohort.memberIds is a list of integers defined by the cohort_member table.
Hibernate automatically creates the schema in our test hsqldb because we have hbm2ddl set to “auto”. The problem arose here because hibernate wasn’t creating a dual primary key on the member_ids table. Digging through the code, I found that Hibernate only assumes the primary keys are the columns that set to “not null”.
The corrected mapping with both columns set to be not-null:
<set name="memberIds" cascade="none" lazy="true" table="cohort_member">
<key column="cohort_id" not-null="true"/>
<element column="patient_id" type="integer" not-null="true"/>
</set>
Now hibernate creates the table with primary keys and hsql is happy again.
August 9th, 2008 at 11:11 am
This is great! We were just running into this issue last week and couldn’t find a solution.
December 12th, 2008 at 5:07 pm
Thanks! Really helped me. Spent a couple hours trying to figure out what this error meant. And with this blog post had it fixed quick!
February 9th, 2010 at 7:36 am
Exactly the same problem I had too, thx!!
March 19th, 2010 at 4:37 pm
Thanks for this post! Solved my problem.