I think you may want to use something similar to this code instead of binding Hibernate as a service:
package util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
In the sample above you'll have access to session factory from your servlet like: HibernateUtil.getSessionFactory()
javapro wrote:
Folks,
I am a newbie with Hibernate and JBoss as well, trying to explore and learn these technologies together. Read articles and books, still have difficulty getting them together working. Do I have to bundle these in WAR or EAR file to get them working? Here's what I have:
Code:
Application Structure
=============
enterpriseApp
- com.modelObjects
- Person.class
- Person.hbm.xml
- META-INF
- hibernate-service.xml
- WEB-INF
- jboss-web.xml
- web.xml
Content of hibernate-service.xml
=====================
<server>
<mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.jca:service=SessionFactory">
<attribute name="DataSourceName">java:/hibernateRnD</attribute>
<attribute name="Dialiect">org.hibernate.dialect.Oracle9Dialect</attribute>
<attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute>
<attribute name="ScanForMappingsEnabled">true</attribute>
</mbean>
</server>
Content of jboss-web.xml
================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.3V2//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_3_2.dtd">
<jboss-web>
<resource-ref>
<res-ref-name>hibernate/hibernateRnD</res-ref-name>
<jndi-name>java:/hibernate/SessionFactory</jndi-name>
</resource-ref>
</jboss-web>
Content of web.xml
=============
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.controller.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/controller/*</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>hibernate/hibernateRnD</res-ref-name>
<res-type>net.sf.hibernate.SessionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Snippet of Codes for Accessing reference to hibernate
===================================
Session session = null;
try{
SessionFactory sessionFactory = getHibernateSessionFactory("java:/hibernate/SessionFactory");
session = sessionFactory.openSession();
}catch(Exception e){
throw new ServiceLocatorException(e);
}
Accessing this throws the following exception:
javax.naming.NameNotFoundException: hibernate not bound
Can someone point out me why this is happening? I am not making EAR but WAR.
Also, I am certain about
name of the
MBean which is name="jboss.jca:service=SessionFactory" - just followed some other example. is
jboss.jca:serice standard? or can be replaced by anything I want? In another word, why jboss? why jca? why :service? Following the documentation, I copied "sessionFactory" from the SessionFactoryName - is it right? what should be there?
It would be a great help if someone can educate me. Thanks.