<?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>indiWiz.com &#187; derby</title>
	<atom:link href="http://indiwiz.com/tag/derby/feed/" rel="self" type="application/rss+xml" />
	<link>http://indiwiz.com</link>
	<description>Subhash&#039;s Tech Log</description>
	<lastBuildDate>Wed, 03 Mar 2010 13:24:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Unit Testing Hibernate Code With Derby</title>
		<link>http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/</link>
		<comments>http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 14:40:14 +0000</pubDate>
		<dc:creator>Subhash Chandran</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[derby]]></category>
		<category><![CDATA[hibernate]]></category>

		<guid isPermaLink="false">http://indiwiz.com/?p=138</guid>
		<description><![CDATA[We faced a scenario which I have faced before: our application uses Hibernate and Oracle Database. And every time we run tests, we had to configure the database specific to that system. I was fed up by this, and explored the possibility of running our code against a test DB. We finalized on Derby. So [...]]]></description>
			<content:encoded><![CDATA[<p>We faced a scenario which I have faced before: our application uses Hibernate and Oracle Database. And every time we run tests, we had to configure the database specific to that system. I was fed up by this, and explored the possibility of running our code against a test DB. We finalized on <a href="http://db.apache.org/derby/">Derby</a>.</p>
<p>So the next step was configuring <tt>hibernate.cfg.xml</tt>. We did this:</p>
<pre class="brush: xml;">
&lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
        &lt;property name=&quot;hibernate.connection.driver_class&quot;&gt;org.apache.derby.jdbc.EmbeddedDriver&lt;/property&gt;
        &lt;property name=&quot;hibernate.connection.url&quot;&gt;jdbc:derby:target/testdb;create=true&lt;/property&gt;
        &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;
        &lt;property name=&quot;dialect&quot;&gt;org.hibernate.dialect.DerbyDialect&lt;/property&gt;
        &lt;property name=&quot;hbm2ddl.auto&quot;&gt;create-drop&lt;/property&gt;
    &lt;/session-factory&gt;
&lt;/hibernate-configuration&gt;
</pre>
<p>In the <tt>hibernate.connection.url</tt> property note the parameter <i>create=true</i>. This ensures when the JDBC layer establishes the connection, the Database will be created if it has not been created earlier. Another important parameter from testing perspective is: <tt>hbm2ddl.auto</tt>. The value set in this parameter is <i>create-drop</i>. This ensures if the tables that are mapped in *.hbm.xml files are not present in the Database, they will be created (during the creation of SessionFactory). And when the SessionFactory is closed, the tables will be dropped. Perfect for testing! Note that, if you want the database tables to persist, just give <i>create</i> instead of <i>create-drop</i>.</p>
<p>So where will the DB be created? This is specified in the <tt>hibernate.connection.url</tt> property. Note the string <i>target/testdb</i>. This is the folder where the DB will be created. Ensure that the relative folder <tt>target/</tt> exists in this case (<tt>testdb</tt> will be automatically created because of the property <tt>create=true</tt>).</p>
<p>In some scenarios we might want to test the correctness of the application by manually verifying the database content. In such situation, as discussed previously, change the <tt>hbm2ddl.auto</tt> property to <i>create</i>. After that connect to that DB using the Derby command-line tool:</p>
<pre>
$ java -cp $JAVA_HOME/db/lib/derby.jar:$JAVA_HOME/db/lib/derbytools.jar \
   org.apache.derby.tools.ij
</pre>
<p>This will open the <tt>ij</tt> prompt. Connect to our DB using it:</p>
<pre>
ij&gt; connect 'jdbc:derby:target/testdb';
ij&gt; select * from __table__;
</pre>
<p>Isn&#8217;t it cool?</p>


<div class="shr-bookmarks shr-bookmarks-expand">
<ul class="socials">
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/&amp;title=Unit+Testing+Hibernate+Code+With+Derby" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/&amp;title=Unit+Testing+Hibernate+Code+With+Derby" rel="nofollow" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-diigo">
			<a href="http://www.diigo.com/post?url=http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/&amp;title=Unit+Testing+Hibernate+Code+With+Derby&amp;desc=We%20faced%20a%20scenario%20which%20I%20have%20faced%20before%3A%20our%20application%20uses%20Hibernate%20and%20Oracle%20Database.%20And%20every%20time%20we%20run%20tests%2C%20we%20had%20to%20configure%20the%20database%20specific%20to%20that%20system.%20I%20was%20fed%20up%20by%20this%2C%20and%20explored%20the%20possibility%20of%20running%20our%20code%20against%20a%20test%20DB.%20We%20finalized%20on%20Derby.%0A%0A" rel="nofollow" title="Post this on Diigo">Post this on Diigo</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/&amp;t=Unit+Testing+Hibernate+Code+With+Derby" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/&amp;title=Unit+Testing+Hibernate+Code+With+Derby&amp;summary=We%20faced%20a%20scenario%20which%20I%20have%20faced%20before%3A%20our%20application%20uses%20Hibernate%20and%20Oracle%20Database.%20And%20every%20time%20we%20run%20tests%2C%20we%20had%20to%20configure%20the%20database%20specific%20to%20that%20system.%20I%20was%20fed%20up%20by%20this%2C%20and%20explored%20the%20possibility%20of%20running%20our%20code%20against%20a%20test%20DB.%20We%20finalized%20on%20Derby.%0A%0A&amp;source=indiWiz.com" rel="nofollow" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/&amp;title=Unit+Testing+Hibernate+Code+With+Derby" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://indiwiz.com/2009/01/05/unit-testing-hibernate-code-with-derby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
