Correct naming convention for boolean values in Hibernate mapping files

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 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();

Follow me on social media

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.