<?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>redstream &#187; Hibernate</title>
	<atom:link href="http://www.redstream.nl/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.redstream.nl</link>
	<description>Pragmatic Integrators</description>
	<lastBuildDate>Fri, 27 Jan 2012 19:31:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Boolean fields with Oracle RDBMS</title>
		<link>http://www.redstream.nl/2008/03/06/using-boolean-fields-with-oracle-rdbms/</link>
		<comments>http://www.redstream.nl/2008/03/06/using-boolean-fields-with-oracle-rdbms/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 20:05:27 +0000</pubDate>
		<dc:creator>Pascal Alma</dc:creator>
				<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://www.pascalalma.net/2008/03/06/using-boolean-fields-with-oracle-rdbms/</guid>
		<description><![CDATA[Like I posted here we are currently busy replacing the Postgres database with an Oracle database. One issue we encountered was the use of boolean fields in the Postgres database, because there is no such type in the Oracle database. &#8230; <a href="http://www.redstream.nl/2008/03/06/using-boolean-fields-with-oracle-rdbms/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2008%2F03%2F06%2Fusing-boolean-fields-with-oracle-rdbms%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2008%2F03%2F06%2Fusing-boolean-fields-with-oracle-rdbms%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Like I posted <a href="http://www.redstream.nl/2008/03/04/comparing-oracle-blobs/">here</a> we are currently busy replacing the Postgres database with an Oracle database. One issue we encountered was the use of boolean fields in the Postgres database, because there is no such type in the Oracle database. So we chose to create a Varchar2(1) field that contains a &#8216;t&#8217; or &#8216;f&#8217;, being &#8216;true&#8217; or &#8216;false&#8217;.<span id="more-88"></span><br />
But since our application has already been written for the biggest part, we are already making use of boolean fields in our Java code. But that&#8217;s where Hibernate comes to the rescue. We are using Hibernate3 as our ORM framework. With Hibernate it is very easy to convert a boolean in the Java code to a char value in the database by extending the class &#8216;CharBooleanType&#8217; to create your own custom type. I have the java code we used for our type here:</p>

<div class="wp_codebox"><table><tr id="p884"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p88code4"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.pascalalma.hibernate.types</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.type.CharBooleanType</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TrueFalseType <span style="color: #000000; font-weight: bold;">extends</span> CharBooleanType <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getTrueString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;t&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getFalseString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;f&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To use this type in our Entity classes I have defined the type with a package annotation (yes, we are using Hibernate annotations at our project). This package scoped annotation is a nice feature since JDK5 which I hadn&#8217;t used or seen before. I have created a file &#8216;package-info.java&#8217; like this:</p>

<div class="wp_codebox"><table><tr id="p885"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p88code5"><pre class="java" style="font-family:monospace;">@TypeDefs<span style="color: #009900;">&#40;</span>
  <span style="color: #009900;">&#123;</span>@TypeDef<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;truefalse-type&quot;</span>,typeClass <span style="color: #339933;">=</span> TrueFalseType.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.pascalalma.hibernate.entity</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.pascalalma.hibernate.types.TrueFalseType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.annotations.TypeDef</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.annotations.TypeDefs</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>(By the way, I read somewhere that the javadoc generator will take the javadoc in this package-info.java as package javadoc, even if there is also a package.html defined in this package)<br />
Now in the entity classes in the package &#8216;net.pascalalma.hibernate.entity&#8217; I can use the defined Type like this:</p>

<div class="wp_codebox"><table><tr id="p886"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code" id="p88code6"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.pascalalma.hibernate.entity</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Column</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Table</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.annotations.Type</span><span style="color: #339933;">;</span>
&nbsp;
@<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aentity+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Entity</span></a>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;MYTABLE&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyTable <span style="color: #009900;">&#123;</span>
&nbsp;
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> id<span style="color: #339933;">;</span>
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aboolean+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Boolean</span></a> indicator<span style="color: #339933;">;</span>
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setName<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	@Id
	@Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;ID&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Along+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Long</span></a> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Column<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;indicator&quot;</span><span style="color: #009900;">&#41;</span>
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;truefalse-type&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aboolean+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Boolean</span></a> getIndicator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> indicator<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIndicator<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aboolean+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Boolean</span></a> indicator<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">indicator</span> <span style="color: #339933;">=</span> indicator<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And that&#8217;s it. More changes aren&#8217;t necessary to solve this issue, so we can continue our conversion untill we run into the next issue :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2008/03/06/using-boolean-fields-with-oracle-rdbms/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Combining enum with EJB3 Entity bean</title>
		<link>http://www.redstream.nl/2007/12/10/combining-enum-with-ejb3/</link>
		<comments>http://www.redstream.nl/2007/12/10/combining-enum-with-ejb3/#comments</comments>
		<pubDate>Mon, 10 Dec 2007 07:20:19 +0000</pubDate>
		<dc:creator>Pascal Alma</dc:creator>
				<category><![CDATA[EJB3]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JBoss AS]]></category>

		<guid isPermaLink="false">http://www.pascalalma.net/2007/12/10/combining-enum-with-ejb3/</guid>
		<description><![CDATA[Do you recognize this: Although you are using a tool to develop your applications for some time, you are suddenly surprised by a certain error message you haven&#8217;t seen before? And you didn&#8217;t do anything different compared to the last &#8230; <a href="http://www.redstream.nl/2007/12/10/combining-enum-with-ejb3/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2007%2F12%2F10%2Fcombining-enum-with-ejb3%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2007%2F12%2F10%2Fcombining-enum-with-ejb3%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Do you recognize this: Although you are using a tool to develop your applications for some time, you are suddenly surprised by a certain error message you haven&#8217;t seen before? And you didn&#8217;t do anything different compared to the last time you used it.<br />
Well, my experience is that in 100 times out of a 100, you did <em>do</em> something different. My last time in this situation was when I wanted to use a Java 1.5 enum type as a field in my EJB3 entity bean.  I had done this before and at first sight everything looked well. Here was what I did:<span id="more-71"></span><br />
Since I deploy my EJB3 application on JBoss 4.2.1 I supplied JBoss with the following Hibernate configuration file:</p>

<div class="wp_codebox"><table><tr id="p7110"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p71code10"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/persistence&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;persistence-unit</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;kpn-jf&quot;</span> <span style="color: #000066;">transaction-type</span>=<span style="color: #ff0000;">&quot;JTA&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>java:/jdbc/my-datasource<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/jta-data-source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.dialect&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;org.hibernate.dialect.MySQLDialect&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.max_fetch_depth&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.hbm2ddl.auto&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;create&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernate.show_sql&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence-unit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/persistence<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>With this file the EJB container creates all database objects when I deploy my application (although this goes against everything I have learned as former &#8216;database&#8217; developer I now just enjoy the simplicity of it).<br />
The enum type I was using was defined as:</p>

<div class="wp_codebox"><table><tr id="p7111"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p71code11"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">net.pascalalma.domains</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> Gender <span style="color: #009900;">&#123;</span>
		U, <span style="color: #666666; font-style: italic;">// Unknown</span>
		M, <span style="color: #666666; font-style: italic;">// Male</span>
		F  <span style="color: #666666; font-style: italic;">// Female</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And the Entity Bean I was using:</p>

<div class="wp_codebox"><table><tr id="p7112"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p71code12"><pre class="java" style="font-family:monospace;">@<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aentity+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Entity</span></a>
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CUSTOMERS&quot;</span><span style="color: #009900;">&#41;</span>
@NamedQueries<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> @NamedQuery<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;findCustomerByCode&quot;</span>, query <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT o FROM Customers o WHERE code = :code&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Customer
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> code<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> initials<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> lastName<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Gender gender<span style="color: #339933;">;</span>
&nbsp;
        ......
&nbsp;
        @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GENDER&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Gender getGender<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> gender<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	......</pre></td></tr></table></div>

<p>Now, like I said before, at first there seemed nothing wrong with my application. I could run it and when I persisted Customers to the database I noticed rows were created in the database, so far so good. However, it went wrong as soon as I started to select an existing row and tried to make an Entity object of it. It was at this point I got the following stacktrace:</p>
<blockquote><p>
Caused by: java.lang.IllegalArgumentException: Unknown name value for enum class net.pascalalma.domains.Gender: 2<br />
        at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:104)<br />
        at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:105)<br />
        at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)<br />
        at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096)<br />
        at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)<br />
        at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)<br />
        at org.hibernate.loader.Loader.getRow(Loader.java:1206)<br />
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)<br />
        at org.hibernate.loader.Loader.doQuery(Loader.java:701)<br />
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)<br />
        at org.hibernate.loader.Loader.doList(Loader.java:2220)<br />
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)<br />
        at org.hibernate.loader.Loader.list(Loader.java:2099)<br />
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)<br />
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)<br />
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)<br />
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)<br />
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)<br />
        at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:80)<br />
        at net.pascalalma.entities.Customer.findCustomerByCode(Customer.java:124)<br />
        at net.pascalalma.services.CustomerServiceBean.validateCustomer(CustomerServiceBean.java:114)<br />
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br />
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)<br />
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)<br />
        at java.lang.reflect.Method.invoke(Method.java:585)<br />
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)<br />
        at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)<br />
        at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)<br />
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)<br />
        at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerIntercep<br />
tor.java:54)<br />
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)<br />
        at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)<br />
        at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)<br />
        at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)<br />
        &#8230; 55 more<br />
Caused by: java.lang.IllegalArgumentException: No enum const class net.pascalalma.domains.Gender.2<br />
        at java.lang.Enum.valueOf(Enum.java:192)<br />
        at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:101)<br />
        &#8230; 88 more
</p></blockquote>
<p>Of course, my first reaction was WTF!?!?! This worked before and why doesn&#8217;t it work right now??<br />
Well, as an experienced developer I knew there had to be something different with the last time I did this (the chance it was the computer thinking, hey, let&#8217;s do something else today seemed not so big ;-) ).<br />
So after a short investigation I found out that I had forgotten an annotation in my Entity bean when defining the Gender field as enum. After adding this:<br />
<strong><code><br />
@Enumerated(EnumType.STRING)<br />
</code></strong><br />
the value of the enum was stored as String, and could be used succesfully when constructing the Entity object.<br />
So this gives &#8216;my mistake&#8217; vs. &#8216;computer mistake&#8217; 101 to 0&#8230;..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2007/12/10/combining-enum-with-ejb3/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Failure trying to catch an UncategorizedSQLException</title>
		<link>http://www.redstream.nl/2007/04/25/failure-trying-catching-an-uncategorizedsqlexception/</link>
		<comments>http://www.redstream.nl/2007/04/25/failure-trying-catching-an-uncategorizedsqlexception/#comments</comments>
		<pubDate>Wed, 25 Apr 2007 04:45:06 +0000</pubDate>
		<dc:creator>Pascal Alma</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Spring Framework]]></category>

		<guid isPermaLink="false">http://www.pascalalma.net/?p=30</guid>
		<description><![CDATA[To store information in our PostgreSQL database we use Spring and Hibernate as I mentioned earlier in my posts. Some of our tables in the database also have GIS information with them. That&#8217;s why we are also using PostGIS extension &#8230; <a href="http://www.redstream.nl/2007/04/25/failure-trying-catching-an-uncategorizedsqlexception/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2007%2F04%2F25%2Ffailure-trying-catching-an-uncategorizedsqlexception%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2007%2F04%2F25%2Ffailure-trying-catching-an-uncategorizedsqlexception%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>To store information in our <a href="http://www.postgresql.org/">PostgreSQL</a> database we use <a href="http://www.springsource.org/">Spring</a> and <a href="https://www.hibernate.org/">Hibernate</a> as I mentioned earlier in my posts. Some of our tables in the database also have GIS information with them. That&#8217;s why we are also using <a href="http://postgis.refractions.net/">PostGIS</a> extension on our database. This GIS information (Geometry) is also reflected in the Hibernate objects we use to persist our data.<br />
We have experienced that this Geometry data can be invalid, in which case the database (JDBC) is throwing an UncategorizedSQLException. This is the case, for example, when a Polygon has a different start and ending point.<br />
In our application we have two kinds of exceptions: the CustomException (thrown and handled by ourselves) and all other exceptions not  handled by us. With this distinction between the exceptions I can decide what to do when an exception occurs, for example, when storing data in the database: is it our CustomException, then it is most probably a problem with this instance and I will move to process the next instance. If there is thrown another Exception, then there is something seriously wrong and I will stop the processing.<span id="more-30"></span><br />
This may sound good in theory but it appeared that when an instance with invalid geometry was stored in the database, the whole process was stopped because there occured an UncategorizedSQLException. The code we used to store our Hibernate object is this:</p>

<div class="wp_codebox"><table><tr id="p3014"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p30code14"><pre class="java" style="font-family:monospace;"> <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> insert<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> CustomObject e<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> CustomException <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
         dao.<span style="color: #006633;">insert</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>UncategorizedSQLException ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// We assume the geometry was invalid</span>
         <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> CustomSystemException<span style="color: #009900;">&#40;</span>CustomErrorMessage.<span style="color: #006633;">CSM_SYS019</span>, ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>DataAccessException ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystemexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SystemException</span></a><span style="color: #009900;">&#40;</span>ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>While this code was working fine in our unit tests it kept failing in our application. After some research we discoverd the (logical, like always) reason for this: since we are using Hibernate to persist our data it is Hibernate that determines <strong>when</strong> the data is posted to the database. And tests showed that this is done when a commit is fired. And that is the big difference between our real-world application and our unit test. In our application we perform a lot of other steps before we fire the commit. So the code has already passed the try-catch before the database is throwing the UncategorizedSQLException. In our unit test we just called this insert and performed a commit right-away, so the exception is thrown inside the try-catch.<br />
So that was the problem but now we had to think about the solution. We came up with 3 options:</p>
<ol>
<li>
 Use AOP to act on every thrown UncategorizedSQLException with a ThrowsAdvice and make it a CustomException.</li>
<li> To call flush() after each insert() and update() so the data is posted in the database right away.
</li>
<li>Perform the validation of the geometry ourselves before we send it to the database.</li>
</ol>
<p>So first we try the flush() because this one was the easiest to implement. While this gave the behaviour we wanted we didn&#8217;t like the fact that we decided when to flush the data instead of letting Hibernate decide this. This could be giving performance issues at some time so we decided to investigate the other solution too.<br />
The second attempt was the AOP option. Because I haven&#8217;t used this before (besides the default out-of-the-box Spring functionlity) I was curious how to get it to work. Well, I got the ThrowsAdvice working for every exception that was thrown in our code, but not for the UncategorizedSQLException. I guess that that is caused by the fact that this exception is thrown by AOP itself, and therefor can not be &#8216;catched&#8217; by another AOP, but I am not sure about this yet.<br />
Finally, because of time-pressure I decided to go for the last option. We were doing all kinds of validations already on the Geometry so I added the one for the polygons. This is performance wise the best solution and I am also very sure what the implications are for the rest of the application.<br />
But someday when I have more time, I am definitely having another look at this AOP thing, because that part is worth some more investigation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2007/04/25/failure-trying-catching-an-uncategorizedsqlexception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate and the famous LazyInitializationException</title>
		<link>http://www.redstream.nl/2007/03/06/hibernate-and-the-infamous-lazyinitializationexception/</link>
		<comments>http://www.redstream.nl/2007/03/06/hibernate-and-the-infamous-lazyinitializationexception/#comments</comments>
		<pubDate>Tue, 06 Mar 2007 20:44:15 +0000</pubDate>
		<dc:creator>Pascal Alma</dc:creator>
				<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://www.pascalalma.net/?p=18</guid>
		<description><![CDATA[When you are working with Hibernate there is a big chance you have (or will) run into the lazy loading exception: org.hibernate.LazyInitializationException: failed to lazily initialize a collection &#8211; no session or session was closed. Well, last week it was &#8230; <a href="http://www.redstream.nl/2007/03/06/hibernate-and-the-infamous-lazyinitializationexception/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.redstream.nl%2F2007%2F03%2F06%2Fhibernate-and-the-infamous-lazyinitializationexception%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.redstream.nl%2F2007%2F03%2F06%2Fhibernate-and-the-infamous-lazyinitializationexception%2F&amp;source=redstreamlive&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>When you are working with Hibernate there is a big chance you have (or will) run into the lazy loading exception: <a href="http://www.hibernate.org/hib_docs/v3/api/org/hibernate/LazyInitializationException.html">org.hibernate.LazyInitializationException</a>: <em><strong>failed to lazily initialize a collection &#8211; no session or session was closed</strong></em>. Well, last week it was my turn. Luckiliy there are <a href="http://www.google.nl/search?hl=nl&#038;q=Hibernate+lazy+loading&#038;meta=">lots and lots of articles and blogs </a>about it on the net so in a short time you will have enough information about it to fill at least a day or two by reading them.<span id="more-2333"></span><br />
In our case we had in one of our objects a OneToMany relation with a child object and it&#8217;s fetchType was set to EAGER. Now this doesn&#8217;t have to be a bad choice but in our case it wasn&#8217;t always necessary to collect the children when the parent was created. So I just set the fetchType to LAZY and tested it&#8230; And there it was: the LazyinitializationException.<br />
So apparently we were trying to load the Object children while there was no (Hibernate) session anymore to which the parent object was attached. After a short search it appeared that the cause of this problem lay in the Transaction demarcation we had defined in Spring (we are using the combination Spring-Hiberate in our project).<br />
We have a &#8216;classic&#8217; layer structure in our components going from business layer -> service layer -> dao layer. Now we had transactions defined at both business layer and service layer. This caused that some objects were created in one transaction, that transaction ended also ending the Hibernate session, and then the object was used, so it tried to lazy load its children but without a Hibernate session&#8230;. et voila: the lazy loading exception. So we did a redesign of our transaction demarcation and this time we only started and ended transactions in our business layer: the correct place according to us. This made the transactional behaviour much more clear but also lead to another problem.<br />
A lot of our unit-test cases are testing at the service layer level. Some of them were failing now since there was no transaction defined at this level. So we decided to make each test method runnning in its own transaction. But we didn&#8217;t want to have to declare all our test classes in the Spring context. So we made a base test class that sets the transactions programmatically. To have this working with <a href="http://testng.org/doc/">TestNG 5.0</a> we made the following base class:</p>

<div class="wp_codebox"><table><tr id="p233316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
</pre></td><td class="code" id="p2333code16"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">net.pascalalma.util.MyContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.Log</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.LogFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.support.ClassPathXmlApplicationContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.orm.hibernate3.HibernateTransactionManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.TransactionStatus</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.support.DefaultTransactionDefinition</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.testng.annotations.AfterMethod</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.testng.annotations.BeforeMethod</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Use this class as base class for your test if you need transactional context
 * in your tests. If this is not needed you should use CoreTest as base class.
 * A transactional is started before the test-method is ran and the transaction will
 * be rolled back when the test method has finished. If you need the data to be committed
 * to the database you can call 'setComplete()'. This will perform a commit on the
 * current transaction.
 *
 * @author Pascal Alma
 *
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TransactionalCoreTest <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Log LOG <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>TransactionalCoreTest.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> HibernateTransactionManager mgr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> TransactionStatus status <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Starts a transaction.
    */</span>
   @BeforeMethod
   <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// Obtaining the Spring context</span>
      ClassPathXmlApplicationContext ctx <span style="color: #339933;">=</span> MyContext.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// Set the transactionManager</span>
      mgr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HibernateTransactionManager<span style="color: #009900;">&#41;</span> ctx.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;transactionManager&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// Start the transaction</span>
      status <span style="color: #339933;">=</span> mgr.<span style="color: #006633;">getTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DefaultTransactionDefinition<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      LOG.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Transaction started&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Commits the changes in the current transaction. At default changes are lost as the
    * transaction is rolled back at the end of the testcase.
    *
    */</span>
   <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setComplete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>status.<span style="color: #006633;">isCompleted</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         mgr.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * Ends the current transaction by rolling back the changes (if any).
    *
    */</span>
   @AfterMethod
   <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> tearDown<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>status.<span style="color: #006633;">isCompleted</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         mgr.<span style="color: #006633;">rollback</span><span style="color: #009900;">&#40;</span>status<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      LOG.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Transaction ended&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So, if we have a test class that needs transactional behaviour we extend from this base class. If we are testing at business layer level, we extend a test base class without this transactional behaviour. This is working fine for us for now, but if you think your solution is better, please let me know! I am always willing to learn new things, especially if these thing are better then the ones I know :-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redstream.nl/2007/03/06/hibernate-and-the-infamous-lazyinitializationexception/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

