- 09 Aug 2008 -

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.

This is in the openmrs category tagged as , , , ,

4 comments »

4 Responses

  1. Kevin Peters Says:

    This is great! We were just running into this issue last week and couldn’t find a solution.

  2. SMH Says:

    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!

  3. BL Says:

    Exactly the same problem I had too, thx!!

  4. hlh Says:

    Thanks for this post! Solved my problem.

Leave a Comment




Your comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.