iam a bit new to hibernates. so i did a simple application using hibernates in struts framework. i had not mentioned any plugin in struts-config.xml. but the application was working fine. iam very confused that how did the struts came to know about the location of my hibernate.cfg.xml and mapping.hbm.xml. but both files was set in my classpath. if anybody knows the answer please inform. thanks in advance.
Joined: Feb 15, 2005
As long as these files are in your classpath, Hibernate can find them.
The important thing to remember when using Hibernate in a web application is that creating the session factory is an expensive and resource intensive task, so you want to make sure you only perform it once and then create a reference to the session factory that you can retrieve somewhere.
One common approach when using Struts is to create a plugin and register it in the struts-config.xml file. This plugin then instatiates the session factory and puts it in Application scope so that it can be retrieved when it's needed.
Another approach is to create a singleton that stores the session factory as a static variable. Either way works.