<?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>Molindo Techblog &#187; Hibernate</title>
	<atom:link href="http://techblog.molindo.at/category/java/hibernate/feed" rel="self" type="application/rss+xml" />
	<link>http://techblog.molindo.at</link>
	<description>Molindo Techblog – formerly known as talk-on-tech.blogspot.com</description>
	<lastBuildDate>Tue, 20 Dec 2011 10:22:19 +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>Using MySQL Collations in Java</title>
		<link>http://techblog.molindo.at/2009/10/using-mysql-collations-in-java.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-mysql-collations-in-java</link>
		<comments>http://techblog.molindo.at/2009/10/using-mysql-collations-in-java.html#comments</comments>
		<pubDate>Wed, 07 Oct 2009 17:22:14 +0000</pubDate>
		<dc:creator>Stefan Fußenegger</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://techblog.molindo.at/?p=125</guid>
		<description><![CDATA[I&#8217;ve recently discovered Stackoverflow as a nice pass-time on the one hand and as a valuable source for answers on the other hand. Normally it takes only a few minutes to get answers for most questions. However, I managed to ask a question that nobody was able to answer yet. The question was about Collations. As I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently discovered <a href="http://stackoverflow.com/" target="_blank">Stackoverflow</a> as a nice pass-time on the one hand and as a valuable source for answers on the other hand. Normally it takes only a few minutes to get answers for most questions. However, I managed to ask <a title="Stackoverflow: Use MySQL collation in Java" href="http://stackoverflow.com/questions/1531107/use-mysql-collation-in-java" target="_blank">a question that nobody was able to answer yet</a>. The question was about <a title="Javadoc: Collator" href="http://java.sun.com/javase/6/docs/api/java/text/Collator.html" target="_blank">Collations</a>. As I&#8217;m suspecting that Collations are a Java feature that is hardly used, I kept working on the problem myself rather then just waiting for an answer on Stackoverflow.</p>
<p><span style="background-color: #ffffff">I&#8217;ve managed to get something working right now. It&#8217;s not completely tested but it should work quite well. What I&#8217;m doing is the following: I parse the<a title="MySQL doc: 9.4. Adding a New Character Set" href="http://dev.mysql.com/doc/refman/5.0/en/adding-character-set.html" target="_blank"> charset files of MySQL</a> (on an Ubuntu system, you can find them in /usr/share/mysql/charsets/) and do the collation based on those files myself rather than using Java&#8217;s built-in collations.</span></p>
<p>EDIT: I&#8217;ve just created <a title="Molindo MySQL Collations on github.com" href="https://github.com/molindo/molindo-mysql-collations" target="_blank">a github project</a> that&#8217;s available as a <a title="at.molindo:molindo-mysql-collations" href="https://oss.sonatype.org/content/repositories/snapshots/at/molindo/molindo-mysql-collations/" target="_blank">Maven artifact from Sonatype&#8217;s OSS repository</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2009/10/using-mysql-collations-in-java.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Detaching and Attaching Persistent Objects on Serialization</title>
		<link>http://techblog.molindo.at/2009/03/detaching-and-attaching-persistent-objects-on-serialization.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=detaching-and-attaching-persistent-objects-on-serialization</link>
		<comments>http://techblog.molindo.at/2009/03/detaching-and-attaching-persistent-objects-on-serialization.html#comments</comments>
		<pubDate>Wed, 25 Mar 2009 15:52:05 +0000</pubDate>
		<dc:creator>Stefan Fußenegger</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Wicket]]></category>

		<guid isPermaLink="false">http://techblog.molindo.at/?p=100</guid>
		<description><![CDATA[Today I&#8217;ve received a mail by someone interested in a serialization mechanism I described some days ago. This mechanism was implemented to avoid dealing with detached Hibernate objects in different HTTP (i.e. Wicket) requests (it&#8217;s not restricted to Hibernate though). The idea is quite simple: detach objects if they are persistent, serialize them if they [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve received a mail by someone interested in <a href="http://stronglytypedblog.blogspot.com/2009/03/wicket-patterns-and-pitfalls-1.html?showComment=1236931620000#c7920901088559233703">a serialization mechanism I described some days ago</a>. This mechanism was implemented to avoid dealing with detached Hibernate objects in different HTTP (i.e. Wicket) requests (it&#8217;s not restricted to Hibernate though). The idea is quite simple: detach objects if they are persistent, serialize them if they are transient, deserialize and attach for the next request &#8211; all transparently. So far, this isn&#8217;t very special and doesn&#8217;t justify such a complicated approach. However, it&#8217;s the only way of attaching and detaching object graphs that consist of transient <strong>and</strong> persistent objects I know.<br />
<span id="more-104"></span>The mechanism uses the magic serialization hooks readResolve() and writeReplace() (<a href="http://www.jguru.com/faq/view.jsp?EID=44039">see jGuru</a>). Knowing these hooks (yeah, knowing &#8230; as it is probably harder to know them than to actually use them), the rest is quite easy. Replace persistent object with a placeholder but serialize regular objects:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> writeReplace<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ObjectStreamException</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isTransient<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</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: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ReplaceHolder<span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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></div></div>

<p>The ReplaceHolder &#8211; isn&#8217;t it a lovely name? &#8211; is responsible for replacing itself with the persistent object we didn&#8217;t want to serialize before:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">class</span> ReplaceHolder <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <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> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> _entityName<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Serializable</span> _id<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> ReplaceHolder<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">Class</span> entity, <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Serializable</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    _entityName <span style="color: #339933;">=</span> entity.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _id <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Serializable</span> 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>
&nbsp;
  @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</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> getEntity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ClassNotFoundException</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span>_entityName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> readResolve<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ObjectStreamException</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> loadEntity<span style="color: #009900;">&#40;</span>getEntity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><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><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">ClassNotFoundException</span> e<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> <span style="color: #003399;">InvalidClassException</span><span style="color: #009900;">&#40;</span>_entityName, e.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And that&#8217;s it. Well, not completely, but <a title="AbstractPersistentObject.java" href="http://techblog.molindo.at/files/AbstractPersistentObject.java">see the attached .java file</a> for details.</p>
<p>There is only one thing Wicket developers have to be aware of. Objects aren&#8217;t serialized between requests &#8211; don&#8217;t confuse it with model detaching. If I remember correctly, there is always one page kept detached but un-serialized. So if one would like to use the serialization, one has to make sure, that the objects are already serialized as soon as the models are detached. For this purpose, I implemented <a title="SerializingModel.java" href="http://techblog.molindo.at/files/SerializingModel.java">a special Model, that serializes its object on detach</a>:</p>

<div class="wp_syntax"><div class="code"><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> detach<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>_object <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">ByteArrayOutputStream</span> bs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span>bs<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>_object<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      _bytes <span style="color: #339933;">=</span> bs.<span style="color: #006633;">toByteArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      _object <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</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><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">IOException</span> e<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> WicketRuntimeException<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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You might say that this feels like a hack &#8211; at least that&#8217;s what I think of it <img src='http://techblog.molindo.at/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  But it works perfectly, especially for complex forms where you add persistent objects to transient ones. Furthermore, you make sure that you&#8217;ll never serialize a huge persistent object graph into your Wicket session, which definetely is a Bad Thing &#8482;.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2009/03/detaching-and-attaching-persistent-objects-on-serialization.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Custom reuseable LoadableDetachableModel for Spring/Hibernate</title>
		<link>http://techblog.molindo.at/2008/05/a-custom-reuseable-loadabledetachablemodel-for-springhibernate.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-custom-reuseable-loadabledetachablemodel-for-springhibernate</link>
		<comments>http://techblog.molindo.at/2008/05/a-custom-reuseable-loadabledetachablemodel-for-springhibernate.html#comments</comments>
		<pubDate>Wed, 21 May 2008 16:28:00 +0000</pubDate>
		<dc:creator>Michael Sparer</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Wicket]]></category>

		<guid isPermaLink="false">http://tech.molindo.at/2008/05/a-custom-reuseable-loadabledetachablemodel-for-springhibernate.html</guid>
		<description><![CDATA[the annoyance org.hibernate.LazyInitializationException - could not initialize proxy - the owning Session was closedorg.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed the problem Generally, detached objects in Hibernate are a bad thing when it comes to reusing objects between web-requests. Detached objects are objects that have been persistent but are not attached [...]]]></description>
			<content:encoded><![CDATA[<h2>the annoyance</h2>
<p><code><br />org.hibernate.LazyInitializationException - could not initialize proxy - the owning Session was closed<br />org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed<br /></code><br />
<h2>the problem</h2>
<p>Generally, detached objects in Hibernate are a bad thing when it comes to reusing objects between web-requests. Detached objects are objects that have been persistent but are not attached to a Hibernate session. They&#8217;re easily produced but can be a mess if it comes to Lazy-Loading or updating them. Just consider the following case</p>
<p><code><br />Foo foo = _myDAO.getFoo(); // attached object</p>
<p>add(new Link("fooLink", new Model(foo)) {</p>
<p>&nbsp;&nbsp;protected void onClick() {<br />&nbsp;&nbsp;&nbsp;&nbsp;Foo foo = (Foo) getModelObject(); // now it's detached as this is called in the next request<br />&nbsp;&nbsp;&nbsp;&nbsp;Bar bar = foo.getBar(); // possible lazy-loading exception if bar gets loaded lazily  </p>
<p>&nbsp;&nbsp;}<br />}<br /></code></p>
<h2>the solution</h2>
<p>First be sure to use the OpenSessionInViewFilter sothat a new hibernate session is opened when the request gets attached and closed when it gets detached.</p>
<p>Then the solution of course is a to use a LoadableDetachableModel that gets a fresh instance from the persistence-layer on each request. Could look like that:</p>
<p><code><br />IModel fooModel = new LoadableDetachableModel() {</p>
<p>&nbsp;&nbsp;protected Object load() {<br />&nbsp;&nbsp;&nbsp;&nbsp;return _myDAO.getFoo();<br />&nbsp;&nbsp;}<br />}</p>
<p>add(new Link("fooLink", fooModel) {</p>
<p>&nbsp;&nbsp;protected void onClick() {<br />&nbsp;&nbsp;&nbsp;&nbsp;Foo foo = (Foo) getModelObject(); // attached as LDM has loaded a fresh instance<br />&nbsp;&nbsp;&nbsp;&nbsp;Bar bar = foo.getBar(); // no lazy loading exception as still attached  </p>
<p>&nbsp;&nbsp;}<br />}<br /></code></p>
<h2>the improvement</h2>
<p>
<p>LoadableDetachableModels are a nice thing but it&#8217;s quite a lot of work to inject your DAO (or service) and then create a new LDM for each of your objects in each and every one of your panels. so we came up with a more generic version of the LoadableDetachableModel. First, let your model-objects implement an Interface, say IPersistentObject with the single method</p>
<p><code><br />&nbsp;&nbsp;Serializable getId();<br /></code></p>
<p>As your object are likely to have a primary key named id it&#8217;ll be no problem anyway. Then create the following class:</p>
<p><code><br />public class PersistentObjectModel extends LoadableDetachableModel {</p>
<p>&nbsp;&nbsp;private static final long serialVersionUID = 1L;</p>
<p>&nbsp;&nbsp;private final Class _clazz;</p>
<p>&nbsp;&nbsp;private final Serializable _id;</p>
<p>&nbsp;&nbsp;@SpringBean(name = "myDao")<br />&nbsp;&nbsp;private IMyDao _myDao;</p>
<p>&nbsp;&nbsp;@SuppressWarnings("unchecked")<br />&nbsp;&nbsp;public PersistentObjectModel(final T object) {<br />&nbsp;&nbsp;&nbsp;&nbsp;super(object);</p>
<p>           // object may be a hibernate proxy, so get the actual class<br />&nbsp;&nbsp;&nbsp;&nbsp;if (object instanceof HibernateProxy) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_clazz = (Class) ((HibernateProxy) object).getHibernateLazyInitializer()<br />                                   .getImplementation().getClass();<br />&nbsp;&nbsp;&nbsp;&nbsp;} else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_clazz = (Class) object.getClass();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;_id = object.getId();<br />&nbsp;&nbsp;&nbsp;&nbsp;// inject the bean<br />&nbsp;&nbsp;&nbsp;&nbsp;InjectorHolder.getInjector().inject(this);<br />&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;public PersistentObjectModel(final Class clazz, final Serializable id) {<br />&nbsp;&nbsp;&nbsp;&nbsp;clazz = clazz;<br />&nbsp;&nbsp;&nbsp;&nbsp;_id = id;<br />&nbsp;&nbsp;&nbsp;&nbsp;// inject the bean<br />&nbsp;&nbsp;&nbsp;&nbsp;InjectorHolder.getInjector().inject(this);<br />&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;protected T load() {<br />&nbsp;&nbsp;&nbsp;&nbsp;// the DAO then simply passes the params to hibernate (or spring's hibernatetemplate<br />&nbsp;&nbsp;&nbsp;&nbsp;return _myDao.getById(_clazz, _id);<br />&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;@SuppressWarnings("unchecked")<br />&nbsp;&nbsp;@Override<br />&nbsp;&nbsp;public T getObject() {<br />&nbsp;&nbsp;&nbsp;&nbsp;return (T) super.getObject();<br />&nbsp;&nbsp;}</p>
<p>}<br /></code></p>
<p>This makes working with hibernate objects really easy. There are two options:</p>
<p></p>
<ol>
<li>Use the object e.g.
<p>setModel(new PersistentObjectModel(foo));</p>
<p></li>
<p>
<li>Use only class and id.
<p>This might happen e.g. if you pass your id using PageParameters (but better encrypt it <img src='http://techblog.molindo.at/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> )<br />setModel(new PersistentObjectModel(Foo.class, 1));</p>
<p></li>
<p></ol>
<p>
<p>Hope that saved you some time writing boilerplate code <img src='http://techblog.molindo.at/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  have fun</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2008/05/a-custom-reuseable-loadabledetachablemodel-for-springhibernate.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Hibernate: could not read column value from result set?</title>
		<link>http://techblog.molindo.at/2008/05/hibernate-could-not-read-column-value-from-result-set.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hibernate-could-not-read-column-value-from-result-set</link>
		<comments>http://techblog.molindo.at/2008/05/hibernate-could-not-read-column-value-from-result-set.html#comments</comments>
		<pubDate>Fri, 16 May 2008 13:26:00 +0000</pubDate>
		<dc:creator>Michael Sparer</dc:creator>
				<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://tech.molindo.at/2008/05/hibernate-could-not-read-column-value-from-result-set.html</guid>
		<description><![CDATA[I got this Error today after changing the primary key of my model objects from int to Serializable (as I wanted all of my objects, even the ones with a String primary key to use the same API, which was limited to int). But when I started the app I got the Error and the [...]]]></description>
			<content:encoded><![CDATA[<p>I got this Error today after changing the primary key of my model objects from int to Serializable (as I wanted all of my objects, even the ones with a String primary key to use the same API, which was limited to int). But when I started the app I got the Error and the following Exception:</p>
<blockquote><p>org.hibernate.type.SerializationException: could not deserialize<br />    at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:217)<br />    at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)<br />    at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)<br />    at org.hibernate.type.SerializableType.get(SerializableType.java:39)<br />    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)<br />    at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)<br />    &#8230;..</p></blockquote>
<p>I even got this one as cause:<br />
<blockquote>Caused by: java.io.EOFException<br />    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2279)<br />    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2748)<br />    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)<br />    at java.io.ObjectInputStream.(ObjectInputStream.java:280)<br />   &#8230;.</p></blockquote>
<p>EOF? wtf? Don&#8217;t get too confused with that EOF, the solution was simply to add the type of the id in the mapping file.</p>
<p>Before:</p>
<blockquote><p>&lt;id name=&#8221;id&#8221;&gt;<br />  &lt;generator class=&#8221;native&#8221;&gt;<br />&lt;/id&gt;</p></blockquote>
<p>After:<br />
<blockquote>&lt;id name=&#8221;id&#8221; <span style="font-weight: bold">type=&#8221;integer&#8221;</span>&gt; &lt;!&#8211; or long or string of whatever your <span style="font-weight: bold">hibernate </span>type is &#8211;&gt;<br />  &lt;generator class=&#8221;native&#8221;&gt;<br />&lt;/id&gt;</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2008/05/hibernate-could-not-read-column-value-from-result-set.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Wicket&#8217;s jmx-panel to view Hibernate&#8217;s Statistics</title>
		<link>http://techblog.molindo.at/2008/01/using-wickets-jmx-panel-to-view-hibernates-statistics.html?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-wickets-jmx-panel-to-view-hibernates-statistics</link>
		<comments>http://techblog.molindo.at/2008/01/using-wickets-jmx-panel-to-view-hibernates-statistics.html#comments</comments>
		<pubDate>Thu, 24 Jan 2008 18:21:00 +0000</pubDate>
		<dc:creator>Stefan Fußenegger</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JMX]]></category>
		<category><![CDATA[Wicket]]></category>

		<guid isPermaLink="false">http://tech.molindo.at/2008/01/using-wickets-jmx-panel-to-view-hibernates-statistics.html</guid>
		<description><![CDATA[The Java Management Extensions (JMX) is perfect to build manageable applications without the need to create any administration frontend. Managed Beans (MBeans) are similar to regular Java Beans. Hibernate offers an MBean-wrapper around its Statistics API which is really interesting to optimize caching. Therefore, I was looking for a simple way to use JMX with [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://en.wikipedia.org/wiki/Java_Management_Extensions">Java Management Extensions (JMX)</a> is perfect to build manageable applications without the need to create any administration frontend. <a href="http://en.wikipedia.org/wiki/Managed_Bean">Managed Beans (MBeans)</a> are similar to regular Java Beans. Hibernate offers an MBean-wrapper around its Statistics API which is really interesting to optimize caching. Therefore, I was looking for a simple way to use JMX with Wicket. Fortunately, <a href="http://wicket.apache.org/">Wicket</a> can also be managed with JMX. All it needs is to drop the wicket-jmx JAR on the classpath &#8211; no configuration needed! It is therefore sufficient to add wicket-jmx to the POM:</p>
<p><code><br />&lt;dependency&gt;<br /> &nbsp;&lt;groupid&gt;org.apache.wicket&lt;/groupid&gt;<br /> &nbsp;&lt;artifactid&gt;wicket-jmx&lt;/artifactid&gt;<br /> &nbsp;&lt;version&gt;1.3.0-SNAPSHOT&lt;/version&gt;<br />&lt;/dependency&gt;<br /></code></p>
<p>But Wicket (more precisely <a href="http://wicketstuff.org/">Wicketstuff</a>) also contains a simple JMX client called <a href="https://wicket-stuff.svn.sourceforge.net/svnroot/wicket-stuff/trunk/wicketstuff-jmx-panel/">wicketstuff-jmx-panel</a>. Using this client is as simple as adding a single component to whatever Wicket-page:</p>
<p>POM:<br /><code><br />&lt;repository&gt;<br /> &nbsp;&lt;id&gt;wicket-stuff-repository&lt;/id&gt;<br /> &nbsp;&lt;name&gt;Wicket-Stuff Repository&lt;/name&gt;<br /> &nbsp;&lt;url&gt;http://www.wicketstuff.org/maven/repository&lt;/url&gt;<br />&lt;/repository&gt;<br />&lt;!-- SNIP --&gt;<br />&lt;dependency&gt;<br /> &nbsp;&lt;groupid&gt;org.wicketstuff&lt;/groupid&gt;<br /> &nbsp;&lt;artifactid&gt;wicketstuff-jmx-panel&lt;/artifactid&gt;<br /> &nbsp;&lt;version&gt;1.3.0-SNAPSHOT&lt;/version&gt;<br />&lt;/dependency&gt;<br /></code></p>
<p>Java:</p>
<p><code><br />add(new JmxPanel("jmx"));<br /></code></p>
<p>However, documentation on this JMX panel is sparse. It took me some time to figure out the following: This panel filters all MBeans that do not have the Wicket-application&#8217;s name in their domain. This behaviour may be changed using an annonymous inner class:</p>
<p><code><br />add(new JmxPanel("jmx") {<br /> &nbsp;protected IDomainFilter getDomainFilter() {<br /> &nbsp; &nbsp;return DOMAIN_FILTER;<br /> }<br />});</p>
<p>private static final IDomainFilter DOMAIN_FILTER = new IDomainFilter() {<br /> &nbsp;private static final long serialVersionUID = 1L;</p>
<p> &nbsp;public boolean accept(String domain) {<br /> &nbsp; &nbsp;// your filter code<br /> &nbsp; &nbsp;return true;<br /> }<br />};<br /></code></p>
<p>It does make sense to filter out some MBeans as there are already some by default, e.g. for java.util.logging which you probably don&#8217;t use at all.</p>
<p>Now it&#8217;s time to add Hibernate&#8217;s statistics MBean. This MBean is can be used per SessionFactory. I therefore decided to do registration and unregistration together with SessionFactory creation. As I am also using <a href="http://springframework.org/">Spring</a>, I went for my own SessionFactoryBean:</p>
<p><code><br />public class MBeanRegisteringLocalSessionFactoryBean extends<br /> &nbsp; &nbsp;org.springframework.orm.hibernate3.LocalSessionFactoryBean {</p>
<p> &nbsp;private String _sessionFactoryName;<br /> &nbsp;private ObjectName _objectName;<br /> &nbsp;private MBeanServer _mbeanServer;</p>
<p> &nbsp;@Override<br /> &nbsp;protected void afterSessionFactoryCreation() throws Exception {<br /> &nbsp; &nbsp;super.afterSessionFactoryCreation();<br /> &nbsp; &nbsp;try {<br /> &nbsp; &nbsp; &nbsp;// register JMX MBean<br /> &nbsp; &nbsp; &nbsp;_objectName = new ObjectName("Hibernate:type=statistics,application="<br /> &nbsp; &nbsp; &nbsp; &nbsp;+ getSessionFactoryName());<br /> &nbsp; &nbsp; &nbsp;_mbeanServer = getMBeanServer();</p>
<p> &nbsp; &nbsp; &nbsp;final StatisticsService mBean = new StatisticsService();<br /> &nbsp; &nbsp; &nbsp;mBean.setSessionFactory(getSessionFactory());<br /> &nbsp; &nbsp; &nbsp;_mbeanServer.registerMBean(mBean, _objectName);</p>
<p> &nbsp; &nbsp;} catch (final Throwable t) {<br /> &nbsp; &nbsp; &nbsp;log.warn("failed to register MBean for SessionFactory: " + _objectName, t);<br /> &nbsp; &nbsp; &nbsp;_objectName = null;<br /> &nbsp; &nbsp; &nbsp;_mbeanServer = null;<br /> &nbsp; &nbsp;}<br /> &nbsp; &nbsp;// if _objectName != null --&gt; MBean was registered<br /> &nbsp;}</p>
<p> &nbsp;@Override<br /> &nbsp;protected void beforeSessionFactoryDestruction() {<br /> &nbsp; &nbsp;if (_objectName != null) {<br /> &nbsp; &nbsp; &nbsp;try {<br /> &nbsp; &nbsp; &nbsp; &nbsp;_mbeanServer.unregisterMBean(_objectName);<br /> &nbsp; &nbsp; &nbsp;} catch (final Throwable t) {<br /> &nbsp; &nbsp; &nbsp; &nbsp;log.warn("failed to unregister MBean for SessionFactory: "<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ _objectName.getDomain());<br /> &nbsp; &nbsp; &nbsp;}<br /> &nbsp; &nbsp;}<br /> &nbsp;}</p>
<p> &nbsp;/* SNIP: getMBeanServer(), getSessionFactoryName(), setSessionFactoryName(String) */<br />}<br /></code></p>
<p>I copied (sorry, there is no nicer way yet) the code for getMBeanServer() from wicket-jmx&#8217;s Initializer class (sorry, can&#8217;t find an online version &#8211; you can get the source using Maven) in order to get the exact same behaviour. Now you only need to replace LocalSessionFactoryBean with MBeanRegisteringLocalSessionFactoryBean in your beans.xml and you are set. In order to enable statistics by default, add these two lines to your hibernate properties:</p>
<p><code><br />hibernate.generate_statistics=true<br />hibernate.cache.use_structured_entries=true<br /></code></p>
<p>That&#8217;s it! Hibernate statistics directly from your webapp:</p>
<p><a href="http://3.bp.blogspot.com/_njTFhNgMPto/R5jkxU0XgoI/AAAAAAAAAAM/J4eV2T4amHU/s1600-h/blog-jmx.JPG"><img style="margin: 0px auto 10px;text-align: center;cursor: pointer;width: 362px;height: 297px" src="http://3.bp.blogspot.com/_njTFhNgMPto/R5jkxU0XgoI/AAAAAAAAAAM/J4eV2T4amHU/s320/blog-jmx.JPG" alt="" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.molindo.at/2008/01/using-wickets-jmx-panel-to-view-hibernates-statistics.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

