I will write a very short note about the state problems I have with hibernate today. There are three states for hibernate objects:Transient, Persistent, Detached. I am not going to describe each of them in full here, googling would give a lot to read about.
My problem was that I chose to use Detached to get the object from DB and then to modify it in the web layer. I thought since it is detached, session has been closed. The object will be modified and later put back to hibernate with a new session. Sounds correct, right? But it always gave me a lot of problems such as complaining about session already closed, etc etc. Briefly, the state of the object is just not right or not retrievable when user modified the object and tried to save it. I used this code:
Foo foo = (Foo) session.load(Foo.class, new Long(1));
this object got in this way is in detached mode. Later, I used this way:
Foo foo = new Foo();
session.load(foo, new Long(1));
this works much nicer since the foo is in transient state. This works great with Web layer since you don’t want to have anything hanging around with hibernate when waiting for web users to get the object info back to the server.
I am not at the bottom of the problem with the detached state, I will explain it more when I find out more. Now I am just happy the transient state object works well.
Hope it helps.