What has worked for me is using a request scoped
managed bean to provide the session (thread local, for the time being), and a
PhaseListener to close it when the rendering is complete. This
approach allows my JSF view to use lists and proxy objects returned directly
from Hibernate (which I think is what you want to do?). I'm not
using Spring for this project right now, but it seems like it wouldn't be too
hard to take advantage of Spring's thread local session feature with a
PhaseListener and simple managed bean that provides
sessions.
- Josh
I also run into problems when using lazy loading due to the fact that an object related to my object was also lazy.
From: Enrique Medina [mailto:[EMAIL PROTECTED]
Sent: Wednesday, August 31, 2005 6:41 AM
To: MyFaces Discussion; [EMAIL PROTECTED]
Subject: Re: JSF + Spring + Hibernate
So I solved it by extending the ListDataModel class with a callback mechanism that allows me to define some kind of logic in every row of the data table (the logic is reattaching the object to the current Hibernate session, of course).
Let me show you some code:
public class LazyListDataModel extends ListDataModel
{
private DataModelCallback dataModelCallback;
private LazyListDataModel()
{
}
private LazyListDataModel(List list)
{
}
public LazyListDataModel(List list, DataModelCallback dataModelCallback)
{
super(list);
this.dataModelCallback = dataModelCallback;
}
public Object getRowData()
{
// Get the object...
Object objeto = super.getRowData();
// ...invoke the callback before returning it.
if (this.dataModelCallback != null)
{
this.dataModelCallback.execute(objeto);
}
return objeto;
}
}
public interface DataModelCallback
{
public abstract void execute(Object objeto);
}
Example of use:
this.politicasDataModel = new LazyListDataModel(this
.busquedaPoliticas(), new DataModelCallback()
{
public void execute(Object objeto)
{
WebappUtils.reasociarObjeto(objeto);
}
});
and the reattachment code:
public static void reasociarObjeto(Object objeto)
{
((DummyDAOHibernateImpl) FacesUtils.getBean("dummyDAO"))
.getHibernateTemplate().lock(objeto, LockMode.NONE);
}
Notice that I use a dummyDAO object just to be able to use Hibernate's DAO support by Spring ;-)
Also my 2 cents ;-)
2005/8/31, Martin Marinschek <[EMAIL PROTECTED]>:A very short hint from my side:
that exception means this object is not in the session anymore. You
can easily reapply it to the session by calling
session.update(object)
If you are sure that your object has not changed since it was last in
the session, you can call:
session.lock(object, LockMode.NONE);
regards,
Martin
On 8/31/05, Werner Punz <[EMAIL PROTECTED]> wrote:
> I think before giving a short answer... I will give you a detailed
> explanation via a links:
>
> http://wiki.apache.org/myfaces/HibernateAndMyFaces?highlight=%28hibernate%29
>
> This small article describes exactly the problems you run into and how
> to solve them...
>
> Werner
>
>
>
> Rick Gruber-Riemer wrote:
> > Hej
> >
> > I use MyFaces (1.0.9), Hibernate (3.0.5) and Spring (1.22) in my webapplication. When I get a list of records from the database in a table or make a new record using a form everything works fine. However when I try to edit a record in a form, I get a org.hibernate.LazyInitializationException error on opening the editform.
> > I understand this has something to do with the Hibernate sessions. And it has nothing to do with MyFaces in particular. And I have found some hints by googeling like Spring's OpenSessionInViewFilter. However I do not have a clue what to change where in the configuration.
> >
> > => Has somebody used JSF+Spring+Hibernate successfully?
> > => Downloadable sample code?
> > => Do I need the jsf-spring integration library?
> >
> > Any hint would be much appreciated ... Rick
> >
> > <hibernate-mapping>
> > <class name="dk.trafikstyrelsen.data.transfer.dto.railsecurity.danafile.ScanJourLink "
> > table="DANAFILE.SCANJOUR_LINK">
> > <id name="id" type="long" column="ROW_ID">
> > <meta attribute="scope-set">protected</meta>
> > <generator class="sequence">
> > <param name="sequence">DANAFILE.SEQNUMBER</param>
> > </generator>
> > </id>
> > <version column="REVISION" name="revision" />
> > <property name="objId" type="string" not-null="true"/>
> > <property name="scanJourNumber" type="int" column="SCANJOUR_NUMBER" not-null="true"/>
> > <property name="notes" type="string" column="NOTES" not-null="false"/>
> > <property name="active" type="yes_no" column="ACTIVE" not-null="true" />
> > <property name="createdBy" type="string" column="CREATED_BY" not-null="true"/>
> > <property name="lastUpdatedBy" type="string" column="LAST_UPD_BY" not-null="true"/>
> > <property name="createdDate" type="timestamp" column="CREATED_DT" not-null="true"/>
> > <property name="lastUpdatedDate" type="timestamp" column="LAST_UPD_DT" not-null="true"/>
> > </class>
> > </hibernate-mapping>
> >
> > <beans>
> > <bean id="danafileHbmDS" class=" oracle.jdbc.pool.OracleDataSource">
> > <property name="driverType"><value>oracle.jdbc.OracleDriver</value></property>
> > <property name="URL"><value>jdbc:oracle:thin:@10.0.0.3:1521:dhdvl</value></property>
> > <property name="user"><value>danafile_web</value></property>
> > <property name="password"><value>styr3ls3n</value></property>
> > </bean>
> >
> > <!-- Hibernate SessionFactories -->
> > <bean id="danafileHbmSF" class=" org.springframework.orm.hibernate3.LocalSessionFactoryBean">
> > <property name="dataSource"><ref local="danafileHbmDS"/></property>
> > <property name="mappingResources">
> > <list>
> > <value>dk/trafikstyrelsen/data/transfer/dto/railsecurity/danafile/ScanJourLink.hbm.xml</value>
> > </list>
> > </property>
> > <property name="hibernateProperties">
> > <props>
> > <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
> > <prop key="hibernate.query.substitutions">yes 'Y', no 'N'</prop>
> > <prop key="hibernate.show_sql">true</prop>
> > </props>
> > </property>
> > </bean>
> >
> > <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
> > <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
> > <property name="sessionFactory"><ref local="danafileHbmSF"/></property>
> > </bean>
> >
> > <bean id="scanJourLinkDAO" class="dk.trafikstyrelsen.data.transfer.dao.railsecurity.danafile.ScanJourLinkHbmDAO ">
> > <property name="sessionFactory"><ref local="danafileHbmSF"/></property>
> > </bean>
> > </beans>
> >
> >
> > ERROR - LazyInitializationException.<init>(19) | could not initialize proxy - the owning Session was closed
> > org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
> > at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java :53)
> > at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
> > at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java :134)
> > at dk.trafikstyrelsen.data.transfer.dto.railsecurity.danafile.ScanJourLink$$EnhancerByCGLIB$$c24a4569.getScanJourNumber(<generated>)
> > at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
> > at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> > at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java :25)
> > at java.lang.reflect.Method.invoke(Method.java:585)
> > at org.apache.myfaces.el.PropertyResolverImpl.getProperty(PropertyResolverImpl.java:419)
> > at org.apache.myfaces.el.PropertyResolverImpl.getValue (PropertyResolverImpl.java:104)
> > at org.apache.myfaces.el.ELParserHelper$MyPropertySuffix.evaluate(ELParserHelper.java:555)
> > at org.apache.commons.el.ComplexValue.evaluate(ComplexValue.java :145)
> > at org.apache.myfaces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:441)
> >
> >
>
>
--
http://www.irian.at
Your JSF powerhouse -
JSF Trainings in English and German

