<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The eflow blog &#187; hibernate</title>
	<atom:link href="http://blog.eflow.org/archives/tag/hibernate/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.eflow.org</link>
	<description>Insert some amazingly witty tagline here</description>
	<lastBuildDate>Fri, 23 Apr 2010 03:19:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Puzzling DBUnit Error</title>
		<link>http://blog.eflow.org/archives/65</link>
		<comments>http://blog.eflow.org/archives/65#comments</comments>
		<pubDate>Sat, 09 Aug 2008 13:36:47 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[openmrs]]></category>
		<category><![CDATA[dbunit]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[hsqldb]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.eflow.org/?p=65</guid>
		<description><![CDATA[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:

&#60;set name="memberIds" cascade="none" lazy="true" table="cohort_member"&#62;
    &#60;key column="cohort_id" /&#62;
    &#60;element column="patient_id" type="integer" /&#62;
&#60;/set&#62;

It looks normal enough.  Cohort.memberIds is a list of integers defined by [...]]]></description>
			<content:encoded><![CDATA[<p>When writing a <a href="http://dbunit.sourceforge.net/">dbunit</a> test for the <a href="http://dev.openmrs.org/browser/openmrs/trunk/test/api/org/openmrs/test/api/CohortServiceTest.java">CohortServiceTest</a>, I encountered this non-informative error:</p>
<blockquote><p>org.dbunit.dataset.NoPrimaryKeyException: COHORT_MEMBER</p></blockquote>
<p>In the <a href="http://dev.openmrs.org/browser/openmrs/trunk/metadata/api/hibernate/org/openmrs/api/db/hibernate/Cohort.hbm.xml">Cohort</a> mapping file the cohort_member table is mapped like any hibernate collection:</p>
<blockquote>
<pre>&lt;set name="memberIds" cascade="none" lazy="true" table="cohort_member"&gt;
    &lt;key column="cohort_id" /&gt;
    &lt;element column="patient_id" type="integer" /&gt;
&lt;/set&gt;</pre>
</blockquote>
<p>It looks normal enough.  <a href="http://resources.openmrs.org/doc/org/openmrs/Cohort.html#getMemberIds()">Cohort.memberIds</a> is a list of integers defined by the cohort_member table.</p>
<p>Hibernate automatically creates the schema in our test hsqldb because we have hbm2ddl set to &#8220;auto&#8221;.  The problem arose here because hibernate wasn&#8217;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 &#8220;not null&#8221;.</p>
<p>The corrected mapping with both columns set to be not-null:</p>
<blockquote>
<pre>&lt;set name="memberIds" cascade="none" lazy="true" table="cohort_member"&gt;
    &lt;key column="cohort_id" not-null="true"/&gt;
    &lt;element column="patient_id" type="integer" not-null="true"/&gt;
&lt;/set&gt;</pre>
</blockquote>
<p>Now hibernate creates the table with primary keys and hsql is happy again.</p>
 <img src="http://blog.eflow.org/wp-content/plugins/feed-statistics.php?view=1&post_id=65" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.eflow.org/archives/65/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>OpenMRS Hibernate Transactions</title>
		<link>http://blog.eflow.org/archives/24</link>
		<comments>http://blog.eflow.org/archives/24#comments</comments>
		<pubDate>Fri, 18 Jan 2008 18:11:32 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[openmrs]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[transacation annotations]]></category>

		<guid isPermaLink="false">http://eflow.org/blog/archives/24</guid>
		<description><![CDATA[I&#8217;m going to post this here as a reminder to other OpenMRS developers (and myself) and to hopefully save a few hours/days of debugging.  The key to getting transactions to work correctly in the webapp is to, well, tell Spring that we&#8217;re in a transaction.  You can do this by simply putting an [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m going to post this here as a reminder to other OpenMRS developers (and myself) and to hopefully save a few hours/days of debugging.  The key to getting transactions to work correctly in the webapp is to, well, tell Spring that we&#8217;re in a transaction.  You can do this by simply putting an <code>@Transactional</code> annotation in the *Service java interface.</p>
<p>Without that key piece of text, the service and its methods will still work.  However, they&#8217;ll work in a readonly kind of way.  All data written to the database will be rolled back when the body of work has completed.  This little error manifested itself recently after our reporting code-a-thon.  During the event we had to create a new API service, which is only done maybe a few times a year.  The service didn&#8217;t get the @Transactional tag and so therefore all database editing failed that went through the ReportService.  The unit tests set up for the object passed, but that is because a unit test happens all within a single transaction &#8212; the rollback is always done after the completion of the test anyway.</p>
<p>I added a note to http://openmrs.org/wiki/OpenMRS_API about the requirement, but I&#8217;m sure there will be an OpenMRS developer in the future struggling with their object not saving or updating in the database and be completely baffled by it.</p>
 <img src="http://blog.eflow.org/wp-content/plugins/feed-statistics.php?view=1&post_id=24" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.eflow.org/archives/24/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
