-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Master/Detail, OpenSessionInView and ThreadLocal
PostPosted: Sat Nov 13, 2004 1:11 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
Hi,

after reading tons of tutorial, books and messages in this forum, I still have a doubt.

In a web application implemented with Struts and Hibernate, what is the right way to implement a sequence of pages dealing with a master/detail object?

Suppose that the logic is more or less the following:
- in the form0 I find a field in which i can put the business key of the document; submission=action0
- in action0 I check if the business key already exists; if the key exists the object is retrieved from database and bound to httpsession; otherwise a new object is created (and bound to httpsession). forward to form1
- in form1 I can see the master (in an html form) and a summary of the detail (suppose this is an invoice; you could have a list of part number, quantity and price for every row).
To modify the master record I have a submit button who send the form fields to an action (action1) and to modify a single row I have to click on a hyperlink that open a form for the single row (containing more fields than those in the summary)
- action1 read the fields from the request and modify the object stored in httpsession
(same for modifications of row: only the object in httpsession is modified)
...
Obviously, after any insertion of rows I need to see that the summary of the detail contains the new row.
Same for modifications: if I change the price in a row, the summary should reflect this change.

Both in new object creation or in modification of an existing object, the operation of persisting to database has to occur ONLY when the user press a SAVE button ath the end of ALL insertions and modifications (after a flow of many html pages).
For this reason the saveOrUpdate method of Hibernate Session is called only at the end of the complete flow of html pages.

Now, suppose that an object is loaded from database for modification (remember that the object maps a master/detail document): I think the only way to implement a flow as described above if to force entire loading of object and related collections.

The option of using OpenSessionInView pattern with a ThreadLocal (to keep Hibernate Session until view rendering is complete) could avoid eager loading of all related collections, but every time I reconnect the session, the object in httpsession is synchronized with database.

Right?


Thank you



Delta


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2004 3:02 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you should use longsession per application transaction
1- setflushmode to never
2- use servlet filter to disconnect / reconnect session, begintransaction and commit underlying db transaction (this will have no effect because of the flushmode)
3- you can use lazy, the jsp(s) will be rendered before disconnection
4- the lonly detail is to manually flush at the transaction application end (last jsp, when user clicks "save").

check servletfilters and hibernateutils from caveatemptor

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 2:14 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
!

Thank you Anthony; I didn't knew setFlushMode.
I'm going to evaluate this approach, even if I don't like long life Sessions.
But I see no other way (with the exception of Hibernate.initialize() )

Thank you again

Delta


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 14, 2004 6:28 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
long session means long according to the life of an application transaction, not more.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.