Hi, my name is Timo Ernst and I am a web expert.

Posts Tagged ‘java’

ZK user group conference in Mannheim, Germany

Posted on: November 22nd, 2011 by Timo

I’ll be speaking on the upcoming ZK user group conference in Mannheim, Germany on December 6th, 2011 (what’s ZK?).

If you enjoy developing rich internet applications with the power of Java, I highly recommend to be there.
If you want to join, just sign up for the event on zkoss.org for free and see three exciting presentations including ones from ZK head Timothy Clare and me.
Also, this is a great opportunity to meet other ZK developers and share some ideas and latest news in the community.

As a little teaser, here is the title sheet for my presentation.

TwitterDiggFacebookShare

.remove() and .contains() not working on your Java set?

Posted on: June 8th, 2011 by Timo

If you suddenly experience problems callling set.remove() and set.contains() on your Java Set instances, you might be using Hibernate, which replaces your Set instance with its own version (PersistentSet), which uses a HashSet internally to store your data.

For example, the problem I faced was the following:

Person.java

public class Person(){

 String _name;

 public Person(String name){
  _name = name;
 }

 @Override
 public String toString(){
  return _name;
 }

 @Override
 public int hashCode(){
  if (_name != null) return _name.hashCode();
  return -1;
 }
}

MyTestApplication.java

public class MyTestApplication{

 public static void main(String[] args){

  Person steve = new Person("Steve");
  Set persons = new Set();
  persons.add(steve);
  System.out.println(persons);

  boolean contains = persons.contains(steve);
  System.out.println(String.valueOf(contains));

  persons.remove(steve);
  System.out.println(persons);

  for (Person person : persons){
   System.out.println("Contains: " + person.toString());
  }

 }
}

The code above prints the following to the console:

[Steve]
false
[Steve]
Contains: Steve

This definitely was not expected.

The reason: If you call set.remove(obj) or set.contains(obj), the internal HashSet will automatically call obj.hashCode() and use the returned value to search for obj.
Normally, this works without any issues, but there is one case where it doesn’t and that’s when:

  • You use Hibernate
  • You load the Set data eagerly
  • You override the obj.hashCode() method

The reason for this is a bug in Hibernate.

For me, a solution was to not override the obj.hashCode() method.
Since this is not a solution for all cases, I recommend to follow the link above and read through the comments. There are quite some workarounds described there. I’m sure one of them will work for you.

Good luck, soldier.

(God, Hibernate! I officially hate you now. It took me a whole day to find the reason for this issue).

TwitterDiggFacebookShare

Correct naming convention for boolean values in Hibernate mapping files

Posted on: May 18th, 2011 by Timo

Ok, this just took me a while to figure out so I thought I’d share it here.

If you have a boolean variable that follows the naming convention “isValue” (e.g. boolean isPerson = false;), then the according mapping file property name may not include the “is” part of the name, for example:

<property name="person" column="is_person" />

Otherwise, Hibernate throws some weird Exceptions.
In my case it was:

Initial SessionFactory creation failed.org.hibernate.InstantiationException: could not instantiate test object FOO
Caused by org.hibernate.InstantiationException: could not instantiate test object BAR
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NoClassDefFoundError: Could not initialize class BAR

Hint: Inside your Java class, the following conventions must be used for getter and setters:

public boolean isPerson();
public void setPerson();

Don’t use:
public void setIsPerson();
!!!

TwitterDiggFacebookShare

New project: zkexamples.com

Posted on: May 17th, 2011 by Timo

I created a new online platform called zkexamples.com.

The goal is to provide really simple and quick examples for common ZK use-cases and requirements (what is ZK?).

The idea is based on flexexamples.com which was a great help to me when I was coding a lot of Adobe Flex.

If anyone has some nice examples to share, feel free to sign up and just post it (requires review and approval by me).

I, personally, try to post every time I learned something new about ZK.
Might help someone else who has the same problem.

TwitterDiggFacebookShare

Quickfix: Hibernate integration for ZK applications

Posted on: April 24th, 2011 by Timo

If you’re using the awesome ZK framework together with Hibernate you might run into some issues where Hibernate behaves a little funky while the application runs.

The trouble’s cause

According to the developers of ZK, the reason for this is ZK’s multi-threaded event model. Whenever an event gets fired, a new event thread is created. This will lead to multiple instances of the Hibernate session class (one per thread) and thus lead to unpredictable behavior.

The quick fix

If you don’t want to read the whole blah-blah in the linked article, here is the quick fix. Just insert the following lines into your zk.xml file and you should be fine:

<!-- Hibernate SessionFactory life cycle -->
<listener>
<description>Hibernate SessionFactory life cycle</description>
<listener-class>org.zkoss.zkplus.hibernate.HibernateSessionFactoryListener</listener-class>
</listener>

<!-- Hibernate OpenSessionInView life cycle -->
<listener>
<description>Hibernate Open Session In View life cycle</description>
<listener-class>org.zkoss.zkplus.hibernate.OpenSessionInViewListener</listener-class>
</listener>
TwitterDiggFacebookShare