Check this document out. It describes the Open Session in View pattern which IMHO works great. I think this is largely what Steve was talking about except, what he said about Filter.destroy() being called after every request is not correct. It's only called when the filter is taken out of service. The session should be closed in the Filter.doFilter() method.

http://hibernate.org/43.html

Jon

----- Original Message ----- From: "steve rock" <[EMAIL PROTECTED]> To: "MyFaces Discussion" <[email protected]>; "Bruno Aranda" <[EMAIL PROTECTED]>
Sent: Tuesday, June 28, 2005 7:03 AM
Subject: Re: SelectItem and Hibernate


What I have done is to create a RequestContextFilter that implements

init(), doFilter(), and destroy() on the javax.servlet.Filter interface.

The destroy() method gets called after every reqeust to my app. Here I
close the db connections. So I keep the connection open for the whole
request. This makes it so I don't have to close the connection in any
other part of my code. We have this running in production and works
real well.

This is configured as a <filter> in my web-app.xml and the
<filter-mapping> is also defined.

We tried keeping the connection open for a whole jsp session but this
didn't work well. We had problems deleting PB because we kept getting
errors like "Cannot delete, an object with this id already exists in
this session".

The only "problem" to closing db connections for every request is that
if you have a persistant bean that you use across several pages and
several requests is that any fields in your PB that are lazy loaded,
i.e. object references to other peristant beans, you will get
LazyInstantiationExcpetion. This is because after the first request
your PB becomes a detached persistance object. You need to know the
hibernate lifecycle of persistant beans to understand this. Read
"Hibernate In Action" or look at the web site documentation.


There are 2 ways to solve this.

1. specify eager fetching in your HQL. Return all the fields you need
in the first request. Once the object becomes detached from the db in
the second request, it will not execute any sql to fetch any
non-loaded fields due to lazy loading.

2. If you still want to use the lazy loading features and not do an
eager fetch in the first request, reattach your detached persistant
bean to the database in the second request. This simply means in you
code call

session.lock( yourPersistantBean, LockMode.NONE) (there are other lock
modes). Then you can access the fields that are lazy loaded and not
throw the exception..

Cheers,
Steve








On 6/28/05, Bruno Aranda <[EMAIL PROTECTED]> wrote:
Hi Lorenzo. Regarding your questions:

> 1)I need translate Hibernate List get from session.find in > []SelectItem?

Yes, you have to create a List of SelectItem objects with the objects
obtained using the query.

> 2) When I use Hibernate with jsf what it is the better session strategy
(ex. close, open). In my getRoles method I must close hibernate session?
I don't know too much about this, but several approximations have been
posted in this list. I hope that anybody with more knowledge than me
will help you on that. Currenty, I am using EJB3 and the strategy to
data access is different,

Regards,

Bruno

2005/6/28, Lorenzo Sicilia <[EMAIL PROTECTED]>:
> Hi to all,
>
> I do some tests with myfaces and hibernate.
> I got some problems about this snippet:
>
>         <x:selectOneMenu id="menu_roles" >
>                 <f:selectItems value="#{userForm.roles}" />
>         </x:selectOneMenu>
>
> my userForm.roles get his data from an Hibernate session.
>
> ...
> public List getRoles() {
>         List roles = null;
>         System.out.println("userForm.getRoles");
>         try {
> Session session = > HibernateSessionFactory.currentSession();
>                 roles = session.find("from Role");
>         } catch (HibernateException e) {
>                 e.printStackTrace();
>         }
>         return roles;
> }
> ...
>
> I get this exception
>
> javax.faces.FacesException: Collection referenced by UISelectItems with
> binding '#{userForm.roles}' and Component-Path : {Component-Path :
> [Class: javax.faces.component.UIViewRoot,ViewId: /role.jsp][Class:
> org.apache.myfaces.component.html.ext.HtmlSelectOneMenu,Id:
> menu_roles][Class: javax.faces.component.UISelectItems,Id: _id0]} does
> not contain Objects of type SelectItem
>
> 1)I need translate Hibernate List get from session.find in > []SelectItem?
>
> 2) When I use Hibernate with jsf what it is the better session strategy
> (ex. close, open). In my getRoles method I must close hibernate > session?
>
> Many Thanks in advance.
>
> regards Lorenzo
>
>



Reply via email to