| « BlazeDS: Use hibernate4gwt as Hibernate adapter | Tester une application multithread » |
Why GWT 1.5 won’t solve your Hibernate issues
I read a lot of GWT blogs, and one common mistake about Hibernate cohabitation is to believe that it is just a syntax issue. People thinks : “GWT only support Java 1.4 syntax, that’s why I cannot send my annotated Hibernate POJO to the client side”, and to conclude : “when GWT 1.5 will be out, Java5 syntax will be supported and I will be able to send my Hibernate beans to the presentation layer”.
This is wrong, absolutely wrong!
The reason is quite simple: Hibernate POJO are not real POJO. The persistence library adds a lot of needed information, such as session factory, by creating a dynamic proxy (with CGLIB or Javassist) around your instance. When you manipulate an Hibernate POJO, you do not do it with an instance of your class, but with a instrumented, derived one!
The second step is that GWT can only serialize (to Javascript) only a subset of the JRE (basically, the java.lang and the java.util packages). That means two things:
- Hibernate classes, such as session factory, are not defined if the GWT supported JRE, so they cannot be serialized
- Derived classes are not Javascript serializable, even if the base class belongs to the supported JRE. That means that even if java.util.Date is supported and serializable, java.sql.Timestamp is not. Furthermore, Hibernate defines its own implementation of the Java collections (PersistentList, PersistentSet and PersistentMap), of course not supported by GWT serialization process.
This is why Javascript serialization of your Hibernate instrumented beans will always fail!
I think that will be one of the big issue of the next GWT release, because only serialization will fail, not compilation. Indeed, your Domain classes are perfectly valid for GWT serialization, but not the associated Hibernate instances.
Hopefully, solutions do exist. You can use Dozer or Beanlib to clone your Domain class (replacing Hibernate collections with their pure Java counterpart) before sending them to the GWT client side, but take care of lazy loading (clone process cause property loading by calling the associated getter).
Or you can have a look to hibernate4gwt, that will do it seamlessly for you!
9 comments
Been looking for a blog.
Impressive. Great execution!
The big issue is that GWT don't support BigDecimal.
Greetings.
BigInteger and BigDecimal are not Hibernate specific issues.
But it is true that the GWT supported JRE is limited and would have to be improved.
I did something similar to add SQL timestamps to the supported JRE (I published the way to do it in this blog_ sorry in french...). It works but need some work.
Bruno
The problems you outlined are not specific to GWT. All of this applies to many other kinds of serialization of Hibernate-enhanced objects: XML over HTTP, Web-Services, RMI, Corba, etc. Whenever there is a client-server communication where both client and server share a subset of domain objects you have to make sure you are not trying to serialize and send Hibernate instances of your objects, otherwise the other side won't be able to deserialize them.
GWT 1.5 is not trying to solve this problem. All it will provide is an ability to use your __original__ annotated POJOs on both sides without having to maintain 1.4 duplicates and that's a big advantage.
I would say "GWT 1.5 won't solve all you Hibernate issues", but it will solve many.
Yegor
But as I mentioned it at the beginning of the article, many, many people seems to think that sending Hibernate POJO to the GWT client side is just a matter of syntax...
Regards
Bruno
1. Language-compatibility (on source and/or byte-code level) on the server and on the client ("the syntax")
2. Serialization of Hibernate-enhanced POJOs ("dynamic proxies")
I don't think there is any misunderstanding among Hibernate users about the second problem, the one you are talking about. We've seen it before even GWT existed. We have solutions for it. The only real problem that's left is the first one and there is no easy solution for it within GWT 1.4 framework.
GWT 1.5 aims to solve the first problem. The second one is already solved. Thus GWT 1.5 will indeed allow me "to send my Hibernate beans to the presentation layer". There is nothing wrong with this statement.
Relating the second problem to GWT is not appropriate. You might as well say "GWT 1.5 won't solve your Hibernate problems. You also have to learn Java language."? Doesn't make much sense, does it?
Yegor
Have a question about integrating acegi with gwt. I tried your blog, can not get it working. Do you mind sending me web.xml, acegi security config file, logon/logoff jsp and sample java codes for handing retrieving username and timeout? Thanks much.
I would like to use hibernate4gwt with my GWT application. I have an integration with Spring with a single entry point. All GWT request are sent to a HandlerAdapter (which extends RemoteServiceServlet) and dispatch the RPC call to the right handler. Would this setup work with your library or I am required to have one servlet for each remote service?
Your project looks very interesting.
Thanks