I am writing a GWT application, using Hibernate at the server side. Right now, I am entirely confused about the correct way to transfer my objects to the client side of the GWT application in the least amount of code. I am using Gilead to avoid having to double the amount of classes in my domain model [1].
First off, my question is how I should be opening sessions and transactions. Initially, I did this on every RPC server call:
// begin rpc call
getCurrentSession
beginTransaction
// ...do stuff
commit
// session is automatically closed
// end rpc call
Since this opens and closes a Session for every RPC call, does this also create a new connection to the database server every time?
Anyway, as soon as I start using lazily loaded collections, I get the following exception using this pattern:
org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:449)`
It seems to me that the session is closed before Gilead gets a chance to serialize the collection object and this causes the exception.
So I tried to remedy this by explicitly opening the Session myself, which prevents it from being closed automatically after every transaction, like so:
openSession
// begin rpc call
beginTransaction
// ...do stuff
commit
// end rpc call
// next rpc call
beginTransaction
// ...etc
However, when I'm doing this, I'm seeing all kinds of funky behavior related to the Session object cache. For one thing, createQuery().executeUpdate() does not seem to invalidate the session cache, although I've read on various sites that it should. When I tried to remedy that by trying to invalidate the session cache by calling various permutations of session.flush(), session.clear() and so on, the next error was a "ClassCastException: null" on serialization deep inside Gilead or Beanlib.
For another thing, when I'm trying something like this:
clients get an object from the server
client modifies object
client sends object back
server calls session.saveOrUpdate()
I'm getting an error like "a different object with the same identifier is already in the session cache".
What is the proper way to do set this sort of thing up? How should I scope my sessions and how should I treat the cache? I can't imagine I'm the only one trying this and having problems with it, but good guides seem to be hard to come by.
[1] http://code.google.com/webtoolkit/articles/using_gwt_with_hibernate.html