-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Hibernate3 with JBoss4.0.4
PostPosted: Fri Jul 07, 2006 1:22 am 
Newbie

Joined: Fri Jul 07, 2006 12:52 am
Posts: 3
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.


Last edited by javapro on Fri Jul 07, 2006 9:04 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: See Docs
PostPosted: Fri Jul 07, 2006 3:27 am 
Beginner
Beginner

Joined: Thu Jul 06, 2006 11:09 pm
Posts: 25
I think there's a way on how to retrieve the SessionFactory object from the MBean instead of using InitialContext.lookup() method. Check on the JBoss/Hibernate docs on how to do this.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 9:09 am 
Newbie

Joined: Fri Jul 07, 2006 12:52 am
Posts: 3
I did try using Session and InitialContext but no success. Please help me.
Thanks.


Top
 Profile  
 
 Post subject: Re: Hibernate3 with JBoss4.0.4
PostPosted: Fri Jul 07, 2006 11:41 am 
Newbie

Joined: Tue Jul 04, 2006 9:58 am
Posts: 15
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.


Top
 Profile  
 
 Post subject: Re: Hibernate3 with JBoss4.0.4
PostPosted: Fri Jul 07, 2006 11:49 am 
Newbie

Joined: Tue Jul 04, 2006 9:58 am
Posts: 15
I forgot to add that if you REALLY want to deploy it as enterprise app you need a structure like this:

myapp.ear:
myapp.sar:
META-INF:
jboss-service.xml
myapp.war:
WEB-INF:
classes/com/modelObjects/
jboss-web.xml
web.xml
myapp-ds.xml

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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 12:17 pm 
Newbie

Joined: Fri Jul 07, 2006 12:52 am
Posts: 3
No I don't want to deploy it as an enterprise app right now. I just would like to get it working as a web application. However, I do like to use it as JBoss service. Please let me know how I can implement Hibernate as JBoss service. However, also, I will try the way you suggested.

I still have a question though that can you please tell me more about the MBean's name's value - name = "jboss.someName:service=AnotherName" ?

is "jboss" is required?
What would the "someName" be? could it be anything I want?
what about the AnotherName - could it be anything? Hibernate or JBoss does not have clear explanation about how and what each of these names are associated with.

I appreciate your dropping by here to help me.
thanks.


Top
 Profile  
 
 Post subject: Hibernate as Service
PostPosted: Fri Jul 21, 2006 12:22 pm 
Newbie

Joined: Mon Dec 19, 2005 12:10 pm
Posts: 5
Create HAR file which will include all POJO and hbm.xml file.Also create hibernate-service.xml file like below :

<server>
<mbean code="org.jboss.hibernate.jmx.Hibernate" name="starwood.ips:service=Hibernate">
<attribute name="DatasourceName">java:/any name</attribute>
<attribute name="Dialect"> your Dialect</attribute>
<attribute name="SessionFactoryName">java:/hibernate/any name</attribute>
<attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>
<attribute name="ShowSqlEnabled">true</attribute>


</mbean>
</server>

That will expose Hibernate as MBean service on JBoss.

Thanks

_________________
Thanks,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.