{"id":204,"date":"2007-09-06T00:00:00","date_gmt":"2007-09-06T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=204"},"modified":"2007-09-06T00:00:00","modified_gmt":"2007-09-06T00:00:00","slug":"Developing Struts Hibernate Plugin","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=204","title":{"rendered":"Developing Struts Hibernate Plugin"},"content":{"rendered":"<p><P>In this section we will develop java code for <B>Struts Hibernate Plugin<\/B>. Our Hibernate Plugin will create Hibernate Session factory and cache it in the servlet context. This strategy enhances the performance of the application.<\/P><br \/>\n<P><B>Source Code Of Hibernate Struts Plugin:<\/B><\/P><br \/>\n<DIV class=java align=left><br \/>\n<TABLE cellSpacing=0 cellPadding=3 bgColor=#ffff99 border=1><br \/>\n<TBODY><br \/>\n<TR><!-- start source code --><br \/>\n<TD vAlign=top noWrap align=left><CODE><FONT color=#7f0055><B>package&nbsp;<\/B><\/FONT><FONT color=#000000>roseindia.net.plugin;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>java.net.URL;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>javax.servlet.ServletContext;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>javax.servlet.ServletException;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.hibernate.SessionFactory;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.hibernate.cfg.Configuration;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.apache.commons.logging.Log;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.apache.commons.logging.LogFactory;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.apache.struts.action.ActionServlet;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.apache.struts.action.PlugIn;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.apache.struts.config.ModuleConfig;<\/FONT><BR><FONT color=#7f0055><B>import&nbsp;<\/B><\/FONT><FONT color=#000000>org.hibernate.HibernateException;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#7f0055><B>public&nbsp;class&nbsp;<\/B><\/FONT><FONT color=#000000>HibernatePlugIn&nbsp;<\/FONT><FONT color=#7f0055><B>implements&nbsp;<\/B><\/FONT><FONT color=#000000>PlugIn&nbsp;<\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>private&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;_configFilePath&nbsp;=&nbsp;<\/FONT><FONT color=#2a00ff>&#8220;\/hibernate.cfg.xml&#8221;<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>\/**<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>*&nbsp;the&nbsp;key&nbsp;under&nbsp;which&nbsp;the&nbsp;<\/FONT><FONT color=#7f7f9f>&lt;code&gt;<\/FONT><FONT color=#3f5fbf>SessionFactory<\/FONT><FONT color=#7f7f9f>&lt;\/code&gt;&nbsp;<\/FONT><FONT color=#3f5fbf>instance&nbsp;is&nbsp;stored<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>*&nbsp;in&nbsp;the&nbsp;<\/FONT><FONT color=#7f7f9f>&lt;code&gt;<\/FONT><FONT color=#3f5fbf>ServletContext<\/FONT><FONT color=#7f7f9f>&lt;\/code&gt;<\/FONT><FONT color=#3f5fbf>.<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;static&nbsp;final&nbsp;<\/B><\/FONT><FONT color=#000000>String&nbsp;SESSION_FACTORY_KEY&nbsp;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>=&nbsp;SessionFactory.<\/FONT><FONT color=#7f0055><B>class<\/B><\/FONT><FONT color=#000000>.getName<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>private&nbsp;<\/B><\/FONT><FONT color=#000000>SessionFactory&nbsp;_factory&nbsp;=&nbsp;<\/FONT><FONT color=#7f0055><B>null<\/B><\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>destroy<\/FONT><FONT color=#000000>()&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>try<\/B><\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>_factory.close<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><FONT color=#7f0055><B>catch<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>HibernateException&nbsp;e<\/FONT><FONT color=#000000>){<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;Unable&nbsp;to&nbsp;close&nbsp;Hibernate&nbsp;Session&nbsp;Factory:&nbsp;&#8221;&nbsp;<\/FONT><FONT color=#000000>+&nbsp;e.getMessage<\/FONT><FONT color=#000000>())<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>init<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>ActionServlet&nbsp;servlet,&nbsp;ModuleConfig&nbsp;config<\/FONT><FONT color=#000000>)&nbsp;<\/FONT><FONT color=#7f0055><B>throws&nbsp;<\/B><\/FONT><FONT color=#000000>ServletException&nbsp;<\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;*************************************&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;****&nbsp;Initilizing&nbsp;HibernatePlugIn&nbsp;&nbsp;&nbsp;**********&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>Configuration&nbsp;configuration&nbsp;=&nbsp;<\/FONT><FONT color=#7f0055><B>null<\/B><\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>URL&nbsp;configFileURL&nbsp;=&nbsp;<\/FONT><FONT color=#7f0055><B>null<\/B><\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>ServletContext&nbsp;context&nbsp;=&nbsp;<\/FONT><FONT color=#7f0055><B>null<\/B><\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>try<\/B><\/FONT><FONT color=#000000>{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>configFileURL&nbsp;=&nbsp;HibernatePlugIn.<\/FONT><FONT color=#7f0055><B>class<\/B><\/FONT><FONT color=#000000>.getResource<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>_configFilePath<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>context&nbsp;=&nbsp;servlet.getServletContext<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>configuration&nbsp;=&nbsp;<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#7f0055><B>new&nbsp;<\/B><\/FONT><FONT color=#000000>Configuration<\/FONT><FONT color=#000000>())<\/FONT><FONT color=#000000>.configure<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>configFileURL<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>_factory&nbsp;=&nbsp;configuration.buildSessionFactory<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f7f5f>\/\/Set&nbsp;the&nbsp;factory&nbsp;into&nbsp;session<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>context.setAttribute<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>SESSION_FACTORY_KEY,&nbsp;_factory<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><FONT color=#7f0055><B>catch<\/B><\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>HibernateException&nbsp;e<\/FONT><FONT color=#000000>){<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;Error&nbsp;while&nbsp;initializing&nbsp;hibernate:&nbsp;&#8221;&nbsp;<\/FONT><FONT color=#000000>+&nbsp;e.getMessage<\/FONT><FONT color=#000000>())<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;*************************************&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>\/**<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>*&nbsp;Setter&nbsp;for&nbsp;property&nbsp;configFilePath.<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>*&nbsp;<\/FONT><FONT color=#7f9fbf>@param&nbsp;<\/FONT><FONT color=#3f5fbf>configFilePath&nbsp;New&nbsp;value&nbsp;of&nbsp;property&nbsp;configFilePath.<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#3f5fbf>*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>public&nbsp;<\/B><\/FONT><FONT color=#7f0055><B>void&nbsp;<\/B><\/FONT><FONT color=#000000>setConfigFilePath<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>String&nbsp;configFilePath<\/FONT><FONT color=#000000>)&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>if&nbsp;<\/B><\/FONT><FONT color=#000000>((<\/FONT><FONT color=#000000>configFilePath&nbsp;==&nbsp;<\/FONT><FONT color=#7f0055><B>null<\/B><\/FONT><FONT color=#000000>)&nbsp;<\/FONT><FONT color=#000000>||&nbsp;<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#000000>configFilePath.trim<\/FONT><FONT color=#000000>()<\/FONT><FONT color=#000000>.length<\/FONT><FONT color=#000000>()&nbsp;<\/FONT><FONT color=#000000>==&nbsp;<\/FONT><FONT color=#990000>0<\/FONT><FONT color=#000000>))&nbsp;{<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#7f0055><B>throw&nbsp;new&nbsp;<\/B><\/FONT><FONT color=#000000>IllegalArgumentException<\/FONT><FONT color=#000000>(<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#2a00ff>&#8220;configFilePath&nbsp;cannot&nbsp;be&nbsp;blank&nbsp;or&nbsp;null.&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>System.out.println<\/FONT><FONT color=#000000>(<\/FONT><FONT color=#2a00ff>&#8220;Setting&nbsp;&#8216;configFilePath&#8217;&nbsp;to&nbsp;&#8216;&#8221;&nbsp;&nbsp;<\/FONT><FONT color=#000000>+&nbsp;configFilePath&nbsp;+&nbsp;<\/FONT><FONT color=#2a00ff>&#8220;&#8216;&#8230;&#8221;<\/FONT><FONT color=#000000>)<\/FONT><FONT color=#000000>;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>_configFilePath&nbsp;=&nbsp;configFilePath;<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;&nbsp;&nbsp;<\/FONT><FONT color=#000000>}<\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#ffffff><\/FONT><BR><FONT color=#3f7f5f>\/*(SessionFactory)&nbsp;servletContext.getAttribute<\/FONT><BR><FONT color=#3f7f5f>(HibernatePlugIn.SESSION_FACTORY_KEY);<\/FONT><BR><FONT color=#3f7f5f>*\/<\/FONT><BR><FONT color=#ffffff>&nbsp;&nbsp;<\/FONT><BR><FONT color=#000000>}<\/FONT><\/CODE> <\/TD><!-- end source code --><\/TR><\/TBODY><\/TABLE><\/DIV><br \/>\n<P><BR>In our plugin class we have define a variable<B> _configFilePath<\/B> to hold the name of <B>Hibernate Configuration file<\/B>. <\/P><br \/>\n<P><FONT color=#0000ff>private String _configFilePath = &#8220;\/hibernate.cfg.xml&#8221;;<\/FONT> <\/P><br \/>\n<P>Following code define the key to store the session factory instance in the Servlet context. <\/P><br \/>\n<P><FONT color=#0000ff>public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();<\/FONT> <\/P><br \/>\n<P>The init() is called on the startup of the Struts Application. On startup the session factory is initialized and cached in the Servlet context. <\/P><br \/>\n<P><FONT color=#0000ff>configFileURL = HibernatePlugIn.class.getResource(_configFilePath);<BR>context = servlet.getServletContext();<BR>configuration = (new Configuration()).configure(configFileURL);<BR>_factory = configuration.buildSessionFactory();<BR>\/\/Set the factory into session<BR>context.setAttribute(SESSION_FACTORY_KEY, _factory);<BR><\/FONT><\/P><br \/>\n<P><B>Changes to be done in struts-config.xml file<\/B> <\/P><br \/>\n<P>Configuring Hibernate with Struts is very simple work it requires you to have hibernate.cfg.xml in your WEB-INF\/classes directory, and to add the following line to the struts-config.xml file. <\/P><br \/>\n<P><FONT color=#0000ff>&lt;plug-in className=&#8221;roseindia.net.plugin.HibernatePlugIn&#8221;&gt;&lt;\/plug-in&gt;<\/FONT> <\/P><br \/>\n<P><B>Testing the Plugin<\/B> <\/P><br \/>\n<P>Build your application and deploy on the tomcat server. Start tomcat server and observe the console output. It should display the following line: <\/P><br \/>\n<DIV align=left><br \/>\n<TABLE cellSpacing=0 cellPadding=0 width=\"59%\" bgColor=#000000 border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD width=\"100%\"><FONT color=#ffffff><B>&nbsp;<BR>&nbsp;log4j:WARN Please initialize the log4j system properly.<BR>*************************************<BR>**** Initilizing HibernatePlugIn **********<BR>*************************************<BR>Aug 7, 2006 10:09:53 AM org.apache.struts.tiles.TilesPlugin initD<BR>&nbsp;&nbsp;<\/B><\/FONT><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><br \/>\n<P>This means you have successfully configured your Struts Hibernate Plugin with struts application. <\/P><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this section we will develop java code for Struts Hibernate Plugin. Our Hibernate Plugin will create Hibernate Session factory and cache it in the servlet context. This strategy enhances the performance of the application. Source Code Of Hibernate Struts Plugin: package&nbsp;roseindia.net.plugin;import&nbsp;java.net.URL;import&nbsp;javax.servlet.ServletContext;import&nbsp;javax.servlet.ServletException;import&nbsp;org.hibernate.SessionFactory;import&nbsp;org.hibernate.cfg.Configuration;import&nbsp;org.apache.commons.logging.Log;import&nbsp;org.apache.commons.logging.LogFactory;import&nbsp;org.apache.struts.action.ActionServlet;import&nbsp;org.apache.struts.action.PlugIn;import&nbsp;org.apache.struts.config.ModuleConfig;import&nbsp;org.hibernate.HibernateException;public&nbsp;class&nbsp;HibernatePlugIn&nbsp;implements&nbsp;PlugIn&nbsp;{&nbsp;&nbsp;&nbsp;private&nbsp;String&nbsp;_configFilePath&nbsp;=&nbsp;&#8220;\/hibernate.cfg.xml&#8221;;&nbsp;&nbsp;&nbsp;&nbsp;\/**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;the&nbsp;key&nbsp;under&nbsp;which&nbsp;the&nbsp;&lt;code&gt;SessionFactory&lt;\/code&gt;&nbsp;instance&nbsp;is&nbsp;stored&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;in&nbsp;the&nbsp;&lt;code&gt;ServletContext&lt;\/code&gt;.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;final&nbsp;String&nbsp;SESSION_FACTORY_KEY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;SessionFactory.class.getName();&nbsp;&nbsp;private&nbsp;SessionFactory&nbsp;_factory&nbsp;=&nbsp;null;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;destroy()&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_factory.close();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}catch(HibernateException&nbsp;e){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Unable&nbsp;to&nbsp;close&nbsp;Hibernate&nbsp;Session&nbsp;Factory:&nbsp;&#8221;&nbsp;+&nbsp;e.getMessage());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;init(ActionServlet&nbsp;servlet,&nbsp;ModuleConfig&nbsp;config)&nbsp;throws&nbsp;ServletException&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;*************************************&#8221;);&nbsp;&nbsp;&nbsp;System.out.println(&#8220;****&nbsp;Initilizing&nbsp;HibernatePlugIn&nbsp;&nbsp;&nbsp;**********&#8221;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Configuration&nbsp;configuration&nbsp;=&nbsp;null;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URL&nbsp;configFileURL&nbsp;=&nbsp;null;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ServletContext&nbsp;context&nbsp;=&nbsp;null;&nbsp;&nbsp;&nbsp;try{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;configFileURL&nbsp;=&nbsp;HibernatePlugIn.class.getResource(_configFilePath);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context&nbsp;=&nbsp;servlet.getServletContext();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;configuration&nbsp;=&nbsp;(new&nbsp;Configuration()).configure(configFileURL);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_factory&nbsp;=&nbsp;configuration.buildSessionFactory();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/Set&nbsp;the&nbsp;factory&nbsp;into&nbsp;session&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context.setAttribute(SESSION_FACTORY_KEY,&nbsp;_factory);&nbsp;&nbsp;&nbsp;}catch(HibernateException&nbsp;e){&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Error&nbsp;while&nbsp;initializing&nbsp;hibernate:&nbsp;&#8221;&nbsp;+&nbsp;e.getMessage());&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;System.out.println(&#8220;*************************************&#8221;);&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;\/**&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Setter&nbsp;for&nbsp;property&nbsp;configFilePath.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;configFilePath&nbsp;New&nbsp;value&nbsp;of&nbsp;property&nbsp;configFilePath.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;setConfigFilePath(String&nbsp;configFilePath)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;((configFilePath&nbsp;==&nbsp;null)&nbsp;||&nbsp;(configFilePath.trim().length()&nbsp;==&nbsp;0))&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;IllegalArgumentException(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8220;configFilePath&nbsp;cannot&nbsp;be&nbsp;blank&nbsp;or&nbsp;null.&#8221;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Setting&nbsp;&#8216;configFilePath&#8217;&nbsp;to&nbsp;&#8216;&#8221;&nbsp;&nbsp;+&nbsp;configFilePath&nbsp;+&nbsp;&#8220;&#8216;&#8230;&#8221;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_configFilePath&nbsp;=&nbsp;configFilePath;&nbsp;&nbsp;&nbsp;&nbsp;}\/*(SessionFactory)&nbsp;servletContext.getAttribute(HibernatePlugIn.SESSION_FACTORY_KEY);*\/&nbsp;&nbsp;} In our plugin class we have define a variable _configFilePath to hold the &hellip; <a href=\"https:\/\/www.strongd.net\/?p=204\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Developing Struts Hibernate Plugin<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-204","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}