Dear Forum member,
When you create a TableNameHome.java using Hibernate tools a method called getSessionFactory() is created. This method returns the SessionFactory object using a JNDI approach.
Considering that I am using Struts, basically, there are two ways to create a SessionFactory object that will be available to all users on each call to the application: Using Servlet filters (
http://www.hibernate.org/43.html) or creating a Hibernate Plug In (
http://www.hibernate.org/105.html).
I am using the second approach: the plug in.
This solution does not use JNDI, but it creates a SessionFactory during Struts initialization and stores this object at the ServletContext.
The point here is: How to retrieve this object (SessionFactory) at a model layer class (the class TableNameHome.java).
The solution provided at
http://www.hibernate.org/105.html shows a way to retrieve the SessionFactory object created by the plug in using:
servletContext.getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY)
This can be easily done in a class derived from org.apache.struts.action.Action, which provides a method to retrieve the ServletContext, but how to perform this at the model layer?
To be more specific in the class created by Hibernate Tools, the class TableNameHome.java
Thanks in advance.