<?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>let x=x &#187; hibernate</title>
	<atom:link href="http://www.crazymcphee.net/x/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.crazymcphee.net/x</link>
	<description>programming idiom and methodology</description>
	<lastBuildDate>Fri, 27 Jan 2012 09:36:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Spring, JPA/JTA, and multiple persistence units, with view transactions</title>
		<link>http://www.crazymcphee.net/x/2011/10/13/spring-jpajta-and-multiple-persistence-units-with-view-transactions/</link>
		<comments>http://www.crazymcphee.net/x/2011/10/13/spring-jpajta-and-multiple-persistence-units-with-view-transactions/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 13:31:32 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[architecture]]></category>
		<category><![CDATA[infrastructure and frameworks]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[persistence]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=655</guid>
		<description><![CDATA[I have grappled with this topic before. Tonight, after 13 hours of struggle, I finally got my web app perfected in this regard. It all started when I needed to start the Transaction out in the view, i.e. as soon as the resource is opened on the HTTP side (rather than when the database service [...]]]></description>
			<content:encoded><![CDATA[<p>I have <a href="http://www.crazymcphee.net/x/2011/02/16/come-back-gavin-king-all-is-forgiven-spring-is-the-new-ejb-2-1/">grappled with this topic</a> before. Tonight, after 13 hours of struggle, I finally got my web app perfected in this regard.</p>
<p>It all started when I needed to start the Transaction out in the view, i.e. as soon as the resource is opened on the HTTP side (rather than when the database service layer is called). I&#8217;m using JPA for a number of reasons;</p>
<ol>
<li>I need to access multiple databases each with a different schema (this means different connections)</li>
<li>They need to have XA transactions.</li>
<li>I&#8217;d like the transactions managed by the container (JTA).</li>
</ol>
<p>JPA provides all these things easily without all the complex Hibernate.xbm.xml mapping files and what-have-you.</p>
<p>The trick to starting the transaction with the web session is to use spring&#8217;s <span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">OpenEntityManagerInViewFilter</span>. Unfortunately I was using  <span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">PersistenceAnnotationBeanPostProcessor</span> to manage my multiple persistence units. The filter wants to know what <span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">EntityManagerFactory </span>it should bind to, and fair enough. But with my minimal configuration, there was no addressable EntityManagerFactory!</p>
<p>The solution was to stop the <span class="Apple-style-span" style="font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px; white-space: pre;">PersistenceAnnotationBeanPostProcessor <span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">from doing the JNDI look ups and bind each PersistenceUnit into the Spring context with a manual JNDI lookup;</span></span></p>
<pre>  &lt;tx:annotation-driven /&gt;
  &lt;tx:jta-transaction-manager /&gt;

  &lt;bean id="pabpp" 
   class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;</pre>
<pre>
  &lt;jee:jndi-lookup id="onePU" jndi-name="persistence/onePU" /&gt;
  &lt;jee:jndi-lookup id="twoPU" jndi-name="persistence/twoPU" /&gt;
  &lt;jee:jndi-lookup id="threePU" jndi-name="persistence/threePU" /&gt;</pre>
<p>then, in the web.xml the ViewFilter could be bound explicitly to the required persistence unit, in this case &#8220;onePU&#8221;. A fuller explanation can be found on my spring source forum post here; <a href="http://forum.springsource.org/showthread.php?115844-OpenEntityManagerInViewFilter-with-JPA-PersistenceAnnotationBeanPostProcessor">http://forum.springsource.org/showthread.php?115844-OpenEntityManagerInViewFilter-with-JPA-PersistenceAnnotationBeanPostProcessor</a></p>
<p>Overall, this is now quite an elegant solution.</p>
<p><strong>UPDATE</strong>. More documentation of the solution and the various configurations at the Spring forum here: <a href="http://forum.springsource.org/showthread.php?115587-Example-for-using-two-databases-w-Spring-amp-JTA-transaction-manager&amp;p=383432#post383432">http://forum.springsource.org/showthread.php?115587-Example-for-using-two-databases-w-Spring-amp-JTA-transaction-manager&amp;p=383432#post383432</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2011/10/13/spring-jpajta-and-multiple-persistence-units-with-view-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Come back Gavin King, all is forgiven (Spring is the new EJB 2.1)</title>
		<link>http://www.crazymcphee.net/x/2011/02/16/come-back-gavin-king-all-is-forgiven-spring-is-the-new-ejb-2-1/</link>
		<comments>http://www.crazymcphee.net/x/2011/02/16/come-back-gavin-king-all-is-forgiven-spring-is-the-new-ejb-2-1/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 12:38:47 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[infrastructure and frameworks]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rants]]></category>
		<category><![CDATA[tools and techniques]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[persistence]]></category>
		<category><![CDATA[poorly attempted humour]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=600</guid>
		<description><![CDATA[I&#8217;ve just spent the past two days trying to make Spring transaction management work with JPA-annotated Hibernate-backed persistence classes that need to have multiple persistence units with transaction propagation REQUIRES_NEW between the two. For a start, the documentation is merely a series of outlines of brief hints. One measly section.The laughably short Spring 3 doco [...]]]></description>
			<content:encoded><![CDATA[<p><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Arial} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Arial; min-height: 17.0px} -->I&#8217;ve just spent the past <em>two days </em>trying to make Spring transaction management work with JPA-annotated Hibernate-backed persistence classes that need to have multiple persistence units with transaction propagation REQUIRES_NEW between the two.</p>
<p>For a start, the documentation is merely a series of outlines of brief hints. One measly section.The laughably short Spring 3 doco section 13.5.1.4 &#8220;Dealing with multiple persistence units&#8221; conveniently omits  the transaction manager configuration from the example. The problem appears to be that the JPA transaction manager only (and compulsorily) deals with <em>one</em> entity manager factory. And an entity manager factory only deals with <em>one</em> persistence unit. So therefore you have to have <em>two</em> JPA transaction managers. Which means the two persistence unit transactions won&#8217;t co-operate properly even though they may share the underlying datasource and are configured through the single persistence unit manager bean.</p>
<p>The above scenario is totally trivial in an EJB 3 container backed with Hibernate, or Eclipselink, as the provider. About one-quarter of the configuration. If I have to use a JTA transaction manager obtained by JNDI lookup from the container, to run a transaction across two JPA persistence units which share the same underlying datasource, why the hell am I using Spring in the first place?</p>
<p>All I wanted was to isolate one set of db transactions from another, use JPA persistence units so the two sets of tables could live easily in two different schemas, and use the annotations to kill the fragile AOP regex-like-but-not-like-regex class and method pattern-matching jiggery pokery from the Spring configuration.</p>
<p>My god, this is such a stonkingly non-trivial forest of horrible configuration and a trial and error morass of filthy swamp miasma &#8230; an equivalent EJB3 backed by Hibernate JPA set up is, by comparison, an almost effortless task. I can produce a unit-tested all singing all dancing multiple persistence unit JPA app running in an EJB 3 container with XA datasources using Hibernate as the JPA implementation in almost no time at all. Getting the same thing with Spring (oh, and running some tests with Jetty inside Maven) is like having all your teeth pulled while you&#8217;re coming down from a three day methamphetamine binge. Getting Spring to run with multiple JPA persistence units on the same JDBC connection (without XA, across the same database connection) with a transaction propagation of REQUIRES_NEW on the entry point of one of the units is a hair-pulling, beard-greying, head-desk banging, co-worker punching, drunken ranting, blood-pressure raising experience of pure horror. Which apparently doesn&#8217;t end.</p>
<p><!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Arial; min-height: 17.0px} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px Arial} -->Die, Spring, die. Can&#8217;t come soon enough as far as I am concerned. It&#8217;s more evil than Oracle. At least you <em>know</em> with Oracle you&#8217;re in for an un-lubed hard and fast backdoor job from Ellison with no reach-around before you even unpack the box. Spring is like a beautiful young sexy soft-porn film that turns into some relentlessly horrific succubus-filled horror film half way through.</p>
<p>Seriously, its enough to make one pine for bloody Weblogic. Spring is the new EJB 2.1.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2011/02/16/come-back-gavin-king-all-is-forgiven-spring-is-the-new-ejb-2-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>ORM-is-Dead meme</title>
		<link>http://www.crazymcphee.net/x/2009/10/19/orm-is-dead-meme/</link>
		<comments>http://www.crazymcphee.net/x/2009/10/19/orm-is-dead-meme/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 07:31:32 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[architecture]]></category>
		<category><![CDATA[infrastructure and frameworks]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[tools and techniques]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[dao]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[orm]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=488</guid>
		<description><![CDATA[I agree with Stephan, and  Aldo; ORMs increasingly get in the way. Collection mapping is one of those &#8220;hello world&#8221; problems. (The &#8220;hello world&#8221; example in the doco looks totally trivial and completely ideal [which is the problem], but suck-in-the-galaxy-greet-it-and-then-map-all-the-stars problem, which is more like what your real app looks like, is far less than [...]]]></description>
			<content:encoded><![CDATA[<p>I agree with <a href="http://codemonkeyism.com/orms/">Stephan</a>, and  <a href="http://www.hatfulofhollow.com/posts/code/farewell-to-orms.html">Aldo</a>; ORMs increasingly get in the way.</p>
<p>Collection mapping is one of those &#8220;hello world&#8221; problems. (The &#8220;hello world&#8221; example in the doco looks totally trivial and completely ideal [which is the problem], but suck-in-the-galaxy-greet-it-and-then-map-all-the-stars problem, which is more like what your real app looks like, is far less than trivial and not so easily achieved with the demonstrated toolkit). In my experience, collection mapping is the cause of all sorts of pain and performance perfidy.</p>
<p>Here&#8217;s a further situation where I find that they <em>really</em> get in the way. Consider integration with existing systems that use highly-normalised database schemas (designed by advanced SQL programmers who know what they are doing in that language).</p>
<p>This is not an uncommon scenario &#8211; we have this system &#8216;A&#8217; which (let&#8217;s say) has all our customer shipping, product, warehousing data in it. We don&#8217;t want to replace it. But we need to extend it so that e.g. available to web systems. Or maybe it needs to be integrated into the big fancy off-the-shelf CRM we&#8217;re installing. Something like that. We need to build some sort of service layer around this system &#8216;A&#8217; so that other systems can access its data and/or functions. It&#8217;s common to think that an ORM around that big complex database schema is going to help, but in my experience, it doesn&#8217;t for many of the reasons Stephan and Aldo list. That sort of highly-normal database schema in my experiences completely kills ORM object representations stone cold. You end up with so many LazyInitialisationExceptions and various other problems it sucks productivity out the developers and performance out of the system.</p>
<p>Also, I get a bit annoyed about ORM abstractions leaking into the web tier, this is especially prevalent with using annotations rather than ORM mapping files.</p>
<p>Different sorts of approaches are sometimes needed to be applied with careful thought in that scenario. Perhaps you need to model the &#8220;intermediate&#8221; domain you need directly, and then use, for example, simple DAO layers which operate directly on the domain you&#8217;ve defined. These DAO layers then might call functions (stored procs) in the database that do the problematic mapping in an efficent relational language (after all it&#8217;s the direct representation of the target data format).</p>
<p>I&#8217;m not saying that you have to do the above in every case, or that an ORM is always wrong, but anyway the point is, ORM isn&#8217;t a magic bullet like any other technology you have to consider carefully. Your application doesn&#8217;t always need one by default.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2009/10/19/orm-is-dead-meme/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Does JPA kill the DAO Pattern?</title>
		<link>http://www.crazymcphee.net/x/2009/01/16/does-jpa-kill-the-dao-pattern/</link>
		<comments>http://www.crazymcphee.net/x/2009/01/16/does-jpa-kill-the-dao-pattern/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 12:30:41 +0000</pubDate>
		<dc:creator>Scot Mcphee</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://www.crazymcphee.net/x/?p=44</guid>
		<description><![CDATA[Oliver Gierke definitely thinks not. And I concur with him. A couple of years ago I got into an running battle with a developer at my old workplace as to whether the JPA architecture abstracted away enough of the persistence to justify eliminating the database layer. My answer was, and still is, certainly not! The [...]]]></description>
			<content:encoded><![CDATA[<p>Oliver Gierke <a href="http://www.olivergierke.de/wordpress/2009/01/se-radio-episode-121-or-mappers/">definitely thinks not</a>. And I concur with him.</p>
<p>A couple of years ago I got into an running battle with a developer at my old workplace as to whether the JPA architecture abstracted away enough of the persistence to justify eliminating the database layer. My answer was, and still is, <em>certainly not!</em></p>
<p>The simple answer to this question is another question &#8211; <em>What if you wanted to replace JPA?</em> The users of the data layer, lets call these objects the SessionLayer, need not, and should not know what sort of persistence layer you&#8217;re using. JPA does manage to abstract away some annoyances with choosing a specific transactional persistence technology just as Hibernate or Toplink. But what if you need to replace database access with a call to a web service? Convert the entire application to use Spring transactions? Send a non-transactional JMS message? Or just write the data in a <em>flat file</em>? </p>
<p>If you have an entirely separate data access layer, which encapsulates your data persistence methods from its users, there&#8217;s only one place to change. It&#8217;s clients need not ever know you made that change. You have <em>isolated the responsibility</em>. JPA, unencapsulated, encourages users of the data layer to consider the method of persistence. Use JPA underneath your data layer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crazymcphee.net/x/2009/01/16/does-jpa-kill-the-dao-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

