1. do:
session.lock(object, LockMode.NONE)
to reassociate a proxy or an entity with a collection.
Note that Hibernate is absolutely no different to JDO or other well-designed ORM solutions in this. This is a standard problem. At least Hibernate gives you _some_ solution for reattachment, unlike JDO.
2. Correct. You are supposed to discard the session after every transaction, whether an exception occurs or not. That is a basic design feature of Hibernate. How could this possibly count as a problem?
3. The way to discard the session-level cache and avoid stale data is by closing the session at the end of the transaction. Just like you are supposed to. To discard the second-level cache, you call sessionFactory.evictXXX().
You do understand the concept of a two-level cache, right? Just because we discard the session, doesn't mean that we lose the second-level cache.
Quote:
For my purposes, Hibernate needs:
1. stable session capable of long life
2. simple way to clear all cache or avoid stale data
3. lazy collections tolerant of nullified sessions. They should auto-reassociate with the new Session. javax.sql.DataSource insulates db apps from JDBC connection loss. Hibernate PersistentCollection needs to use a similar approach, don't store the session, store a way to always get one! AKA Factory pattern. :-)
1. Why????
2. Session.clear(), Session.close(), SessionFactory.evictXXX()
3. It is impossible to auto-associate a POJO with the current threadlocal session without resorting to AOP-style interception. This requires control of the build or classloader, which we don't want. Since we won't do it for POJOs, we must not corrupt our semantics by having auto-association for some objects (eg. collections, proxies) and not others. And you don't need it anyway: Session.lock() is not hard to do.