thank you for your promt reply!
sorry, I should have made my design goals clear. I have read the pattern section, but I couln't find a solution for the following design requirement:
I want to encapsulate all DB-access code in a DAO layer, so that the client (web) app is not aware of the underlying Hinbernate layer. This means : no session.open/close in servlet, servlet filter etc. on top of the business object layer.
Clients must not have access to any underlying integration layer API (including Hibernate, JDBC, EJBs etc...) in order to avoid dependencies.
the client should be able to use persistent objects in the following way:
Code:
try {
// begin JTA transaction
Order order = OrderFactory.findOrderForClient(28);
for (Iterator iter = order.getOrderItems().iterator(); iter.hasNext()) {
OrderItem item = (OrderItem) iter.next();
... do lots of things with order items
}
... do lots of things with other persistent objects (maybe using a single thread local hibernate session internally)
... use some EJBs
// commit JTA transaction
} catch (BusinessException e) { // JTA rollback}
Note: The client is not required to open/close any Hibernate session object.
My idea was the following DAO desing (which doesnt seem to work with lazy proxies):
DAOCode:
class OrderDAO {
public Order findForClient(int clientID) throws DAOException {
try {
Session session = DB.getSessionViaThreadLocalPattern();
Order o = select Order ... where client = clientID ...
DB.releaseSessionAsSoonAsTransactionCommitts(session);
return o;
} catch (HibernateException e) {throw new DAOException()}
}
...
public load(Set itemsProxy) {
Session session = DB.getSessionViaThreadLocalPattern();
Hibernate.initialize(itemsProxy); // throws "no session" exception
DB.releaseSessionAsSoonAsTransactionCommitts(session);
}
}
Despite using the above DAO I'd still like to be able to use Hibernates lazy proxies AFTER the session has been closed in a previous request:
Business ObjectCode:
class Order {
OrderDAO dao = new OrderDAO();
private Set orderItems;
Iterator getOrderItems() {
if (!dao.isInitialized(this, orderItems))
dao.load(orderItems); // reconnect to Hibernate Session and replace proxy with real data. DOESNT WORK!
return orderItems;
}
}
If I cant replace proxies after the session has been closed in a previous reques I have to eagerly load all business object's associations at once in the first user request.
It looks like that I can use Hibernate with the above DAO design only for business objects that have composite semantics (i.e. detail associations are always loaded when the master is loaded).
Are there any designs which support the above design, completely encasulates Hibernate in a DAO layer and allow lazy loading ?!
Thanks
Dirk