Sunday, November 30, 2008

Hibernate property "reflection"

If you ever get IllegalArgumentException in BasicPropertyAccessor and every type in class and in hibernate mapping are set right, then you should look for method that has similar name to property setter that is rising the exception.

private String tagExpression;
public void setTagExpression(String exp) {...}
public void getTagExpression() {...}
public boolean isTagExpression() {...}
Stack trace:
20:08:09,369 ERROR BasicPropertyAccessor:94 - IllegalArgumentException in class: [some class], setter method of property: tagExpression
20:08:09,376 ERROR BasicPropertyAccessor:98 - expected type: java.lang.String, actual value: java.lang.Boolean
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of [some class].tagExpression
Hibernate is somehow calling isTagExpression() instead of getTagExpression() when setting tagExpression. So the simplest solution to this unpleasant situation is to rename isTagExpression() to something else like isTagExp() or isTExpression(). The hibernate then find correct property getter by reflection.

It took me hours to find out what is going on with hibernate, hope it helps someone in a same sticky situation.

No comments: