<?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>Shaun Abram &#187; Maven</title>
	<atom:link href="http://www.shaunabram.com/tag/maven/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shaunabram.com</link>
	<description>Java and Technology weblog</description>
	<lastBuildDate>Tue, 27 Jul 2010 23:14:10 +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>Running Hibernate unit tests with Maven</title>
		<link>http://www.shaunabram.com/hibernate-maven-unit-tests/</link>
		<comments>http://www.shaunabram.com/hibernate-maven-unit-tests/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 08:37:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenSource]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=42</guid>
		<description><![CDATA[I recently converted a project I have been working on to use Maven. After setting up all the dependencies in the pom, I got everything compiling fine but ran into problems getting the unit tests to pick up the hibernate config (hibernate.cfg.xml) and hibernate mapping (*.hbm.xml) files. With hindsight, it is straightforward, but it took [...]]]></description>
			<content:encoded><![CDATA[<p>I recently converted a project I have been working on to use <a href="http://maven.apache.org/">Maven</a>. After setting up all the dependencies in the pom, I got everything compiling fine but ran into problems getting the unit tests to pick up the hibernate config (hibernate.cfg.xml) and hibernate mapping (*.hbm.xml) files. With hindsight, it is straightforward, but it took me a while to figure it out so I thought I&#8217;d post the solution here.</p>
<p>Initially, I had my hibernate.cfg.xml file in the following directory</p>
<p style="padding-left: 30px;">my-app/src/main/java</p>
<p>And when I first tried to run my hibernate unit tests (mvn test), I got this error:</p>
<p style="padding-left: 30px;">SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found</p>
<p>Some checking of the maven docs and some forums revealed the following possible solutions:<br />
1) Copy the hibernate.cfg.xml file to</p>
<p style="padding-left: 30px;">my-app/target/classes</p>
<p>This works, however I think it is a hack as the target folder is generated rather than being somewhere you should actually manually create files.<br />
2) The next solution I found suggested moving the hibernate.cfg.xml to</p>
<p style="padding-left: 30px;">my-app/src/main/resources</p>
<p>This works well as the contents of this folder are copied to the base level of the my-app/target/classes folder so it is basically a &#8216;more correct&#8217; solution than the first.<br />
3) The next and I think best solution was to create a new, duplicate hibernate.cfg.xml inside</p>
<p style="padding-left: 30px;">my-app/src/test/resources</p>
<p>This allows a different hibernate.cfg.xml file to be used for testing and, for example, facilitates connecting to a different database (such as <a href="http://hsqldb.org/">hsqldb</a>) for testing while continuing to use your regular database (such as <a href="http://www.mysql.com/">MySQL</a>) for the app itself.</p>
<p>A couple of points to note:</p>
<p>1) The hibernate mapping files (i.e. the *.hbm.xml files) should also be in the resources folder (in whatever directory structure you choose) to ensure that these too are accessible by the unit tests.</p>
<p>2) I am not (yet) using Spring with the hibernate components, which would likely have changed the above setup.</p>
<p>Refs/links</p>
<p style="padding-left: 30px;"><a href="http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR">http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR</a></p>
<p style="padding-left: 30px;"><a href="http://www.mail-archive.com/users@maven.apache.org/msg54720.html">http://www.mail-archive.com/users@maven.apache.org/msg54720.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/hibernate-maven-unit-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
