Hibernate relations and event listeners

Posted by Jens on January 24, 2012

In my current project I work on a Java EE application with a hibernate backend. The applications supports two databases MSSql and Oracle. The development started with MSSql. One of my tasks was to migrate the application to Oracle. We expected some problems with Spring ACLs which uses SQL instead of OR Mapping. To fix that I just had to write some custom SQL for Oracle.

After handling the ACLs I got transaction problems with oracle, we hadn’t with MSSql. After some tests and research I found out that to fix this I had to add a entityManager.flush(); to the persist and merge method of our entity manager. I was happy to resolved the issue an started our test suite.

Then I run into an exception like this:

  1. Caused by:
  2. org.hibernate.AssertionFailure: collection [someEntity] was not processed by flush()

I got this exception on our local H2-database on MSSql and Oracle. After some research I found this issue in the hibernate Jira:

HHH-2763: Allow initialization during flush

In the first place I ignored it because the entry was from 2007. After some more research and some tests I found out that our problem looked similar to the issue reported to hibernate. We had relations and we had listeners in the form of PrePersistCallbacks and PreUpdateCallbacks. I couldn’t believe that an issue from 2007 was still not fixed.

My next step was to write a test case to reproduce the error. The test case is very domain specific so I can’t post it here.

I found out that the bug was already fixed in grails. With this knowledge it was easy to came to this solution:

  1. package app.dao;
  2.  
  3. import org.hibernate.HibernateException;
  4. import org.hibernate.ejb.event.EJB3FlushEventListener;
  5. import org.hibernate.event.EventSource;
  6.  
  7. /**
  8.  * This file is heavily inspired by the PatchedDefaultFlushEventListener from grails:
  9.  * https://github.com/grails/grails-core/blob/master/grails-hibernate/src/main/groovy/org/codehaus/groovy/grails/orm/hibernate/events/PatchedDefaultFlushEventListener.java
  10.  *
  11.  * Patches Hibernate to prevent this issue
  12.  * https://hibernate.onjira.com/browse/HHH-2763
  13.  *
  14.  * TODO: Remove this patch when HHH-2763 is resolved
  15.  *
  16.  */
  17.  
  18. public class PatchedFlushListener extends EJB3FlushEventListener {
  19.  
  20.   private static final long serialVersionUID = -6913148400994182443L;
  21.  
  22.   @Override
  23.   protected void performExecutions( EventSource session ) throws HibernateException {
  24.     session.getPersistenceContext().setFlushing( true );
  25.     try {
  26.       super.performExecutions( session );
  27.     } finally {
  28.       session.getPersistenceContext().setFlushing( false );
  29.     }
  30.   }
  31. }

To make this work in our application I had to add this spring configuration to our applicationContext-datasource.xml:

  1. <property name="jpaPropertyMap">
  2.       <map>
  3.         <entry key="javax.persistence.validation.factory" value-ref="validator" />
  4.         <!–  call PatchedFlushListener on flush event. Patches Hibernate to prevent this issue
  5.            https://hibernate.onjira.com/browse/HHH-2763
  6.            TODO: Remove this patch when HHH-2763 is resolved
  7.          –>
  8.       <entry key="hibernate.ejb.event.flush" value="app.dao.PatchedFlushListener"/>
  9.       </map>
  10. </property>

The problem occurred with Hibernate 3.6.7.Final which was the latest stable release by the time I run into it. It should be fixed in Hibernate 4.0.0.Final or later which final release was in december.

4Gewinnt Vanilla for iOS

Posted by Jens on January 18, 2012

Christoph just released his first iOS game. A awesome iOS implementation of connect four called 4Gewinnt Vanilla. On the strongest game level there is almost no chance to win against the computer. But in the middle levels it’s a perfect entertaining game.

The app is designed for both iPhone and iPad and available in the app store:

4Gewinnt Vanilla in the app store




Article published in OBJEKTspektrum

Posted by Jens on January 17, 2012

Together with André I wrote an article for the german computer journal Objektspektrum. The article with the name “Code-Flavours: Nützliche Java-Idiome” is published in the current issue.

A code flavour is the opposite of a code smell. Similar to anti-pattern v.s. design pattern. In the article we discuss some useful code flavours for the java programming language. The concepts might work for other languages to.

You can order the issue of object spectrum here or just buy the article as a pdf version here.


It’s all about the user interface

Posted by Jens on January 14, 2012

To get better at developing user interfaces, I decided last year to learn more about javascript. Like most developers I already used javascript for years, but never invested the time to “really” learn the language.

Studying a language works best for me when I have a serious project to develop. After surfing around I came up with the idea to extract the travel map I developed for journizer.com to a Autobahn81 product for sale.

The new version of the google api is not compatible with the old one. So the new travel map item was developed nearly from scratch. The guys at google did a really great job improving the api. On of the best things on Gmaps api V3 is, that you don’t need an api key anymore.

Travel map pro makes it super easy to include a a google maps based travel map on your website. The live preview of travel map pro is available here. The photoshop items for the markers and a great documentation is included. You can buy it as travel map pro javascript or travel map pro php for some dollars.

travelmap_screenshot

Ruby based backend for PEP

Posted by Jens on January 13, 2012

Last year I developed a nice and small ruby based backend for the new website of Pieger Electronic Publishing. It’s a small design company based in Grafenau, Germany.

If you need your next design job to get done. Feel free to contact them. I really recommend there work.

Now there new website is finally online. They have this awesome parrot in a different situation on every sub page. It’s really worth to click trough all of there pages. Just awesome!