воскресенье, 28 сентября 2008 г.

hibernate4gwt and processing all class fields

Today I was stuck working on my project which employed both hibernate and GWT technologies and thus also needed to use hibernate4gwt lib.

This is a great library that makes it possible to integrate two other great framewroks into one project. But as usual it has some caveats =)

It worked great for my model classes passing them from server side to JS client side. With one exception. All model object's ids were dropped. At first I thought this might have to do with @Id or @GeneratedValue annotations ... but that even sounds silly - why would you expect a cloning lib to drop a plain simple Long value because it has an @Id annotation? =)

Half an hour and 15Mb source code downloads later I found out the real cause. The library that hibernate4gwt uses for actual cloning of hibernate-enchanced objects to POJO is called beanlib (specifically beanlib-hibernate) and deep inside, in a method called net.sf.beanlib.provider.BeanPopulator#populate it uses all defined setters on a given class to populate it. Apparently I haven't defined a setter for id field - which caused the beanutil lib to drop it when converting to POJO.

So the moral of this story is: always create both getters and setters for all you fields on a class that you expect to pass to some 3rd party bean handling libs =)

Комментариев нет: