EntityManager injection (JPA, Hibernate, Tomcat)
romendo Aug 9, 2008 9:09 PMHi there,
I am using Seam with JPA and Hibernate in Tomcat. I am using a custom DataModel in order to provide paging, sorting, and filtering. The code has originally been developed for use with Tomahawk DataTable. I am now in the process of moving this from MyFaces to Seam.
In the old version I used a ThreadLocal variable to store the EntityManager and corresponding Hibernate Session. However, this does not work with Seam. That's why I have added the following to components.xml:
<factory name="session"
scope="STATELESS"
auto-create="true"
value="#{em.delegate}"/>I then attempt to inject the session into a JavaBean that has session scope. The JavaBean is used to access the DataModel:
@DataModel protected FilterablePagedDataModel<Object> dataModel
I also use a custom DataModels object in order to support the DataModel instances with @DataModel:
@Name("org.jboss.seam.faces.dataModels")
@Install(precedence=FRAMEWORK)
@Scope(STATELESS)
@BypassInterceptors
public class CustomDataModels extends DataModels {
@Override
public DataModel getDataModel(Object value) {
if (value instanceof DataModel) {
return (DataModel) value;
} else {
return super.getDataModel(value);
}
}
}Everything seems to work fine except for one thing. The session gets reset to null. Using the debugger I see the following is happening:
- session gets injected properly.
- the getDataModel of my JavaBean is called (this does not yet load any data)
- session gets injected again, this time as null.
- HtmlDataTable.isRowAvailable() gets called: Since no data has been loaded yet the JavaBean attempts to load data. Since the session is null this fails with a NPE.
- session gets injected properly.
- HtmlDataTable.getRowCount() gets called from DataModelBinder.getSelection(DataModel,DataModel).
- the getDataModel of my JavaBean is called
- finally data is loaded via DataModelBinder.getWrappedData.
- session gets injected again, this time as null
- session gets injected properly yet again
- session gets injected again, this time as null
The end result is the display of the error page with the NPE as reason.
I use a redirect when accessing the page that contains the dataTable.
My question: Why does Seam inject the session with value null? Is there anything I can do to avoid that?
Thanks,
Roland