|
Integrating Struts and Hibernate |
|
import
org.apache.commons.logging.LogFactory;
import
org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import
org.apache.struts.config.ModuleConfig;
import org.hibernate.HibernateException;
public class HibernatePlugIn implements PlugIn
{
private String _configFilePath = “/
hibernate.cfg.xml”;
/**
* the key under which the
<code>SessionFactory</code> instance is
stored
* in the <code>ServletContext</code>.
*/
public static final String
SESSION_FACTORY_KEY
= SessionFactory.class.getName();
private SessionFactory _factory = null;
public void destroy() {
try{
_factory.close();
}catch(HibernateException e){
System.out.println(“Unable to close
Hibernate Session Factory: “ +
e.getMessage());
}
}
public void init(ActionServlet servlet,
ModuleConfig config) throws ServletException
{
System.out.println(“*********************
****************”);
System.out.println(“**** Initilizing
HibernatePlugIn **********”);
Configuration configuration = null;
URL configFileURL = null;
ServletContext context = null;
try{
configFileURL =
HibernatePlugIn.class.getResource
|
|
(_configFilePath);
context = servlet.getServletContext();
configuration = (new
Configuration()).configure(configFileURL);
_factory =
configuration.buildSessionFactory();
//Set the factory into session
context.setAttribute(SESSION_FACTORY_KEY,
_factory);
}catch(HibernateException e){
System.out.println(“Error while initializing
hibernate: “ + e.getMessage());
}
System.out.println(“*****************
********************”);
}
/**
* Setter for property configFilePath.
* @param configFilePath New value of
property configFilePath.
*/
public void setConfigFilePath(String
configFilePath) {
if ((configFilePath == null) ||
(configFilePath.trim().length() == 0)) {
throw new IllegalArgumentException(
“configFilePath cannot be blank or null.”);
}
System.out.println(“Setting ‘configFilePath’ to
‘“ + configFilePath + “‘...”);
_configFilePath = configFilePath;
}
/*(SessionFactory)
servletContext.getAttribute
(HibernatePlugIn.SESSION_FACTORY_KEY);
*/
}
In our plugin class we have define a variable
_configFilePath to hold the name of
Hibernate Configuration file.
private String _configFilePath = “/
hibernate.cfg.xml”;
|
|
Dec
2007 | Java Jazz Up | 57 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
52,
53 ,
54,
55,
56,
57,
58,
59,
60,
61,
62,
63 ,
64,
65 ,
66 ,
67 ,
68 ,
69 ,
70 ,
71 ,
72 ,
73 ,
74 ,
75 ,
76 ,
77 ,
78 ,
79 ,
80 ,
81 ,
82 , Download PDF |
|
|
|
|
|
|
|
|
|