<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Hibernate Sessions in Two Tier Rich Client Applications</title>
	<atom:link href="http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/</link>
	<description>Softwaredevelopment, Projectmanagement, Qualitymanagement and all things &#34;schauderhaft&#34;</description>
	<lastBuildDate>Tue, 07 Feb 2012 23:24:32 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Jens Schauder</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-4548</link>
		<dc:creator>Jens Schauder</dc:creator>
		<pubDate>Wed, 30 Nov 2011 15:53:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-4548</guid>
		<description>@Adam Dyga Yes that is basically what we do. We have a central place that stores the session for each Frame. If the session throws an exception it should get closed and removed and all the entities of the frame should get reloaded using a fresh session</description>
		<content:encoded><![CDATA[<p>@Adam Dyga Yes that is basically what we do. We have a central place that stores the session for each Frame. If the session throws an exception it should get closed and removed and all the entities of the frame should get reloaded using a fresh session</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Dyga</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-4547</link>
		<dc:creator>Adam Dyga</dc:creator>
		<pubDate>Wed, 30 Nov 2011 15:18:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-4547</guid>
		<description>This is interesting solution. I&#039;m trying to do similar thing using JPA/Hibernate, but I&#039;m concerned about one thing. If EntityManager throws an exception, according to docs, it should be immediately closed. How do you handle this situation if there are still some domain objects in the memory (GUI, eg. tree), that may still attempt to lazy-load data? Do you create new EntityManager and reset it in all places where it&#039;s used?</description>
		<content:encoded><![CDATA[<p>This is interesting solution. I&#8217;m trying to do similar thing using JPA/Hibernate, but I&#8217;m concerned about one thing. If EntityManager throws an exception, according to docs, it should be immediately closed. How do you handle this situation if there are still some domain objects in the memory (GUI, eg. tree), that may still attempt to lazy-load data? Do you create new EntityManager and reset it in all places where it&#8217;s used?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mathias De Belder</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-3051</link>
		<dc:creator>Mathias De Belder</dc:creator>
		<pubDate>Fri, 11 Feb 2011 23:56:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-3051</guid>
		<description>@Blaz

I can&#039;t just copy and paste our code on the web. However, the idea of using remote lazy loading has also been explored by other people. There&#039;s a fine article on &lt;a href=&quot;http://www.theserverside.com/news/1363571/Remote-Lazy-Loading-in-Hibernate&quot; rel=&quot;nofollow&quot;&gt;TheServerSide&lt;/a&gt; that pretty much does exactly the same as what we have in place (apparently the source code snippets there are clipped, so be sure to check out the HTML source). Having lazily loaded entities is a bit harder than lazily loaded collections, so I&#039;d advise to start off with the latter, which is actually pretty easy to implement. For lazily loaded entities you should really look at the Hibernate source code itself (start with https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/main/java/org/hibernate/proxy/pojo/BasicLazyInitializer.java) to grok how it&#039;s done and what parts you need to substitute. Poking around with a debugger in Hibernate&#039;s source code is quite instructive to get a clear picture of what&#039;s actually going on.

Then it&#039;s simply a matter of replacing the bits in your Hibernate Configuration object:

EntitityTuplizerFactory f = new Configuration().configure(&quot;some.cfg.xml&quot;).getEntityTuplizerFactory();
f.registerDefaultTuplizerClass(EntityMode.POJO, RemoteLazyEntityTuplizer.class);


Where RemoteLazyEntityTuplizer is a PojoEntityTuplizer specialization that returns a special ProxyFactory returning JavaAssist proxies (implementing MethodHandler and Hibernate&#039;s own LazyInitializer) that know how to get to your EJB facade when they need to be initialized.</description>
		<content:encoded><![CDATA[<p>@Blaz</p>
<p>I can&#8217;t just copy and paste our code on the web. However, the idea of using remote lazy loading has also been explored by other people. There&#8217;s a fine article on <a href="http://www.theserverside.com/news/1363571/Remote-Lazy-Loading-in-Hibernate">TheServerSide</a> that pretty much does exactly the same as what we have in place (apparently the source code snippets there are clipped, so be sure to check out the HTML source). Having lazily loaded entities is a bit harder than lazily loaded collections, so I&#8217;d advise to start off with the latter, which is actually pretty easy to implement. For lazily loaded entities you should really look at the Hibernate source code itself (start with <a href="https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/main/java/org/hibernate/proxy/pojo/BasicLazyInitializer.java">https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/main/java/org/hibernate/proxy/pojo/BasicLazyInitializer.java</a>) to grok how it&#8217;s done and what parts you need to substitute. Poking around with a debugger in Hibernate&#8217;s source code is quite instructive to get a clear picture of what&#8217;s actually going on.</p>
<p>Then it&#8217;s simply a matter of replacing the bits in your Hibernate Configuration object:</p>
<p>EntitityTuplizerFactory f = new Configuration().configure(&#8220;some.cfg.xml&#8221;).getEntityTuplizerFactory();<br />
f.registerDefaultTuplizerClass(EntityMode.POJO, RemoteLazyEntityTuplizer.class);</p>
<p>Where RemoteLazyEntityTuplizer is a PojoEntityTuplizer specialization that returns a special ProxyFactory returning JavaAssist proxies (implementing MethodHandler and Hibernate&#8217;s own LazyInitializer) that know how to get to your EJB facade when they need to be initialized.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Blaz Demsar</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-3016</link>
		<dc:creator>Blaz Demsar</dc:creator>
		<pubDate>Mon, 07 Feb 2011 16:28:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-3016</guid>
		<description>@Mathias 
I would like to try the sollution you are proposing, however, since I am relatively new in Hibernate, I am struggling with the implementation. Could you share a snapshot of the code? 
LG
Blaz</description>
		<content:encoded><![CDATA[<p>@Mathias<br />
I would like to try the sollution you are proposing, however, since I am relatively new in Hibernate, I am struggling with the implementation. Could you share a snapshot of the code?<br />
LG<br />
Blaz</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Illa Ades</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-2901</link>
		<dc:creator>Illa Ades</dc:creator>
		<pubDate>Tue, 18 Jan 2011 20:10:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-2901</guid>
		<description>I am really thankful to this topic because it really gives great information :~,</description>
		<content:encoded><![CDATA[<p>I am really thankful to this topic because it really gives great information :~,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sola</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-1149</link>
		<dc:creator>sola</dc:creator>
		<pubDate>Thu, 18 Nov 2010 11:36:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-1149</guid>
		<description>I am rewriting my pure-Swing application as a Netbeans RCP application.

I believe I will create a DB Persistence Service Module which will be responsible for downloading data objects and writing them back to the database. This should handle lazy-loading as well. Ideally, this will have a server-side counterpart and the communication between them will be HTTP-only in order to avoid cutting through firewalls. This should be able to handle HTTP proxies.

Hibernate should not be visible, only JPA style annotations should be used on the data objects.

If someone knows about such persistence service (using JPA, with server and client side, communication is with HTTP) please, let me know (soltesz(point}andras (at) gmail dotcom)..</description>
		<content:encoded><![CDATA[<p>I am rewriting my pure-Swing application as a Netbeans RCP application.</p>
<p>I believe I will create a DB Persistence Service Module which will be responsible for downloading data objects and writing them back to the database. This should handle lazy-loading as well. Ideally, this will have a server-side counterpart and the communication between them will be HTTP-only in order to avoid cutting through firewalls. This should be able to handle HTTP proxies.</p>
<p>Hibernate should not be visible, only JPA style annotations should be used on the data objects.</p>
<p>If someone knows about such persistence service (using JPA, with server and client side, communication is with HTTP) please, let me know (soltesz(point}andras (at) gmail dotcom)..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sola</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-1148</link>
		<dc:creator>sola</dc:creator>
		<pubDate>Thu, 18 Nov 2010 11:29:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-1148</guid>
		<description>@Mathias
Great ideas for solving Hibernate problems.

I am also rewriting my rich client application to work with completely detached objects and hacking lazy loading back into the game (not using Hibernate&#039;s default lazy loading).

By the way: A Hibernate session dying on you while saving a new/modified object back to the database is the ugliest thing you can imagine. It is extremely hard to shake the session back to shape in order to continue using it. We do the following when saving: 
- We merge every dirty object into a session created specifically for the save operation (save session)
- We save the objects, this may succeed or fail
- We drop the &quot;save session&quot;  and merge objects back to the original session
- If an object was meant to be saved but the save operation failed, we clear the ID field which was filled out during the save operation

This way the &quot;save session&quot; will always be dropped after the save attempt and cannot cause further problems after it failed once.
The original, &quot;query session&quot; will remain intact, even if the save operation failed.</description>
		<content:encoded><![CDATA[<p>@Mathias<br />
Great ideas for solving Hibernate problems.</p>
<p>I am also rewriting my rich client application to work with completely detached objects and hacking lazy loading back into the game (not using Hibernate&#8217;s default lazy loading).</p>
<p>By the way: A Hibernate session dying on you while saving a new/modified object back to the database is the ugliest thing you can imagine. It is extremely hard to shake the session back to shape in order to continue using it. We do the following when saving:<br />
- We merge every dirty object into a session created specifically for the save operation (save session)<br />
- We save the objects, this may succeed or fail<br />
- We drop the &#8220;save session&#8221;  and merge objects back to the original session<br />
- If an object was meant to be saved but the save operation failed, we clear the ID field which was filled out during the save operation</p>
<p>This way the &#8220;save session&#8221; will always be dropped after the save attempt and cannot cause further problems after it failed once.<br />
The original, &#8220;query session&#8221; will remain intact, even if the save operation failed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mathias De Belder</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-991</link>
		<dc:creator>Mathias De Belder</dc:creator>
		<pubDate>Sun, 10 Oct 2010 12:09:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-991</guid>
		<description>You can have lazy loading with already closed Sessions. We use it quite a bit at work which admittedly uses a three-tier architecture. But there is no reason you can&#039;t program against &quot;SessionFacades&quot; that do their work locally through DAOs which in turn delegate to Hibernate with a couple of connections from the client machines to the DB server.

The trick is to write your own EntityTuplizer (and register it with your Configuration object) that returns an implementation of the HibernateProxy interface (or implementations of the PersistentXYZ collection interfaces). When Hibernate creates a lazy reference it will always store the name of the association and a serializable PK blob. That can then be used to fetch the relation &quot;JIT&quot; in a &lt;strong&gt;new&lt;/strong&gt; Session. For the collection interfaces you can get away with plain Java but for the case where the destination of a relation is a POJO you&#039;ll need to use a bytecode generation library like Javassist (like Hibernate uses already).

Say you have a one-to-many relationship. You query the one-side with some constraints and you display the resultant entities in a JList. When the user clicks on an item, the &quot;many&quot;-relationship (usually a Set, but can be a POJO as well) is traversed. Under the covers, the PersistentXYZ collection will go through a special facade with the PK and association name to fetch the association just-in-time. The downside to this is that you can block the EDT with DB-access unknowingly. Usually the access is &lt;250ms, so quite acceptable. If it&#039;s really becoming a problem after profiling we wrap the access in a SwingWorker and show a fancy &quot;please wait&quot; animation.

Of course, at some level you still have to be cognizant that the objects you&#039;re dealing with are eventually fetched from a database. That&#039;s why we have methods on our SessionBeans that deliver a fairly &quot;wide&quot; object-graph for the client to begin with. The rest is fetched lazily (our Swing clients go through our app-server, but as noted before, the clients can use Hibernate directly).

We work exclusively with detached entities and merge them back in again when a user saves something in a form. This has the &quot;downside&quot; of needing a good equals() and hashCode() implementation but that will pay itself back big time down the line (although I had to fight many battles at work to make people not write hashCode() implementations that NPEd all the time). If you prime the L1 cache with a good query the merge will only have to perform insert/update/deletes. Fetching and persisting your entities is still not completely transparent (methods to select/save a graph of objects on your facades) but still a whole lot better than the procedural nightmare of EJB2 with DTOs everywhere and different methods on your SessionBeans to traverse associations.</description>
		<content:encoded><![CDATA[<p>You can have lazy loading with already closed Sessions. We use it quite a bit at work which admittedly uses a three-tier architecture. But there is no reason you can&#8217;t program against &#8220;SessionFacades&#8221; that do their work locally through DAOs which in turn delegate to Hibernate with a couple of connections from the client machines to the DB server.</p>
<p>The trick is to write your own EntityTuplizer (and register it with your Configuration object) that returns an implementation of the HibernateProxy interface (or implementations of the PersistentXYZ collection interfaces). When Hibernate creates a lazy reference it will always store the name of the association and a serializable PK blob. That can then be used to fetch the relation &#8220;JIT&#8221; in a <strong>new</strong> Session. For the collection interfaces you can get away with plain Java but for the case where the destination of a relation is a POJO you&#8217;ll need to use a bytecode generation library like Javassist (like Hibernate uses already).</p>
<p>Say you have a one-to-many relationship. You query the one-side with some constraints and you display the resultant entities in a JList. When the user clicks on an item, the &#8220;many&#8221;-relationship (usually a Set, but can be a POJO as well) is traversed. Under the covers, the PersistentXYZ collection will go through a special facade with the PK and association name to fetch the association just-in-time. The downside to this is that you can block the EDT with DB-access unknowingly. Usually the access is &lt;250ms, so quite acceptable. If it&#039;s really becoming a problem after profiling we wrap the access in a SwingWorker and show a fancy &quot;please wait&quot; animation.</p>
<p>Of course, at some level you still have to be cognizant that the objects you&#039;re dealing with are eventually fetched from a database. That&#039;s why we have methods on our SessionBeans that deliver a fairly &quot;wide&quot; object-graph for the client to begin with. The rest is fetched lazily (our Swing clients go through our app-server, but as noted before, the clients can use Hibernate directly).</p>
<p>We work exclusively with detached entities and merge them back in again when a user saves something in a form. This has the &quot;downside&quot; of needing a good equals() and hashCode() implementation but that will pay itself back big time down the line (although I had to fight many battles at work to make people not write hashCode() implementations that NPEd all the time). If you prime the L1 cache with a good query the merge will only have to perform insert/update/deletes. Fetching and persisting your entities is still not completely transparent (methods to select/save a graph of objects on your facades) but still a whole lot better than the procedural nightmare of EJB2 with DTOs everywhere and different methods on your SessionBeans to traverse associations.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jens Schauder</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-411</link>
		<dc:creator>Jens Schauder</dc:creator>
		<pubDate>Tue, 23 Mar 2010 19:00:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-411</guid>
		<description>Hi Vlad,
Your assessment of validation unique constraints is absolutely correct. And for this reason I strongly recommend the use of database constraints, which do essentially what you describe with pretty good performance and very well tested.

Yet &#039;enforcing&#039; this kind of constraint in the gui, without using the locking catches many cases of constraint violation, which would otherwise only get caught by the database, rendering the session unusable. 

There is only a small gap left, for another user entering the same value at almost the same time and the database constraint takes care of that.

The performance cost of this approach is very limited, since we only use a select on an indexed column and no locking. 

So for us it was a nice compromise between performance, ease of implementation and usability.</description>
		<content:encoded><![CDATA[<p>Hi Vlad,<br />
Your assessment of validation unique constraints is absolutely correct. And for this reason I strongly recommend the use of database constraints, which do essentially what you describe with pretty good performance and very well tested.</p>
<p>Yet &#8216;enforcing&#8217; this kind of constraint in the gui, without using the locking catches many cases of constraint violation, which would otherwise only get caught by the database, rendering the session unusable. </p>
<p>There is only a small gap left, for another user entering the same value at almost the same time and the database constraint takes care of that.</p>
<p>The performance cost of this approach is very limited, since we only use a select on an indexed column and no locking. </p>
<p>So for us it was a nice compromise between performance, ease of implementation and usability.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vlad Sadilovskiy</title>
		<link>http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/comment-page-1/#comment-410</link>
		<dc:creator>Vlad Sadilovskiy</dc:creator>
		<pubDate>Tue, 23 Mar 2010 18:19:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.schauderhaft.de/2008/09/28/hibernate-sessions-in-two-tier-rich-client-applications/#comment-410</guid>
		<description>Hi. 

I wanted to say that validating some type of constraints is not possible even conceptually in the application layer without locking tables that are involved in such validation. Consider unique key validation. The only way that guaranties uniqueness and would not throw a unique constraint violation is to do the following sequence of actions. 1. Lock the table from modifications 2. Query this table for duplicates of an incoming data. 3. If no duplicates found, insert incoming data. 4. Unlock the table or commit transaction. This is workable, but in my opinion cannot be a solution for concurrent multi-user enterprise level application.

Any other algorithm may render the assumptions of querying for duplicates invalid at the time of the insert.</description>
		<content:encoded><![CDATA[<p>Hi. </p>
<p>I wanted to say that validating some type of constraints is not possible even conceptually in the application layer without locking tables that are involved in such validation. Consider unique key validation. The only way that guaranties uniqueness and would not throw a unique constraint violation is to do the following sequence of actions. 1. Lock the table from modifications 2. Query this table for duplicates of an incoming data. 3. If no duplicates found, insert incoming data. 4. Unlock the table or commit transaction. This is workable, but in my opinion cannot be a solution for concurrent multi-user enterprise level application.</p>
<p>Any other algorithm may render the assumptions of querying for duplicates invalid at the time of the insert.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

