7 Replies Latest reply on Jun 17, 2008 2:13 PM by Matt Cook

    Trying to get JBoss and Hibernate to integrate

    Matt Cook Newbie

      Running

      JBossAS 4.2.0
      JBoss Portal 2.6.5
      Hibernate 3

      Hey all, I have been working on trying to get JBoss to work with hibernate and MySQL. I have successfully accessed the database through a datasource, but I cant seem to configure hibernate correctly. My session objects are always returned to me as null. Any help would be appreciated.

      Here are my configs and src code:

      This is in the deploy directory of the server.
      SSApp-mysql-ds.xml:

      <?xml version="1.0" encoding="UTF-8"?>
      
      <!-- $Id: mysql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ -->
      <!-- Datasource config for MySQL using 3.0.9 available from:
      http://www.mysql.com/downloads/api-jdbc-stable.html
      -->
      
      <datasources>
       <local-tx-datasource>
       <jndi-name>SupplySideDS</jndi-name>
       <connection-url>jdbc:mysql://localhost:3306/myDB</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class>
       <user-name>tiki</user-name>
       <password>god</password>
       <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
      
       <!-- should only be used on drivers after 3.22.1 with "ping" support
       <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
       -->
       <!-- sql to call when connection is created
       <new-connection-sql>some arbitrary sql</new-connection-sql>
       -->
       <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
       <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
       -->
      
       <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->
       <metadata>
       <type-mapping>mySQL</type-mapping>
       </metadata>
       </local-tx-datasource>
      </datasources>
      


      This next file is in the meta-inf of the project
      hibernate.cfg.xml:
      <hibernate-configuration xmlns="urn:jboss:hibernate-deployer:1.0">
      
       <session-factory name="java:/hib-timers/SessionFactory" bean="jboss.test.har:service=Hibernate,testcase=TimersUnitTestCase">
       <property name="datasourceName">java:/SupplySideDS</property>
       <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
       <property name="sessionFactoryInterceptor">org.jboss.test.hibernate.SimpleInterceptor</property>
       <property name="hbm2ddlAuto">create</property>
       <depends>jboss:service=Naming</depends>
       <depends>jboss:service=TransactionManager</depends>
       </session-factory>
      
      </hibernate-configuration>
      


      HibernateUtil.java:
      import org.hibernate.HibernateException;
      import org.hibernate.Session;
      import org.hibernate.SessionFactory;
      import org.hibernate.cfg.AnnotationConfiguration;
      
      
      public class HibernateUtil {
      
       private static final SessionFactory sessionFactory;
      
       static {
      
       try {
      
       AnnotationConfiguration config = new AnnotationConfiguration();
       config.addAnnotatedClass( User.class );
       config.configure();
      
       sessionFactory = config.buildSessionFactory();
      
       } catch (Throwable ex) {
      
       // Log exception!
       throw new ExceptionInInitializerError(ex);
       }
       }
      
       public static Session getSession() throws HibernateException {
      
       return sessionFactory.openSession();
       }
      
      }
      


      User.java:
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.Table;
      
      
      @Entity
      @Table( name="UserInfo" )
      public class User {
      
       private String Username;
       private String EMail;
       private String FirstName;
       private String LastName;
       private String Phone;
      
       //private Set<Vendor> vendor;
      
       @Id
       public String getUsername() {
      
       return Username;
       }
      
       @Column( name="EMail" )
       public String getEMail() {
      
       return EMail;
       }
      
       @Column( name="FirstName" )
       public String getFirstName() {
      
       return FirstName;
       }
      
       @Column( name="LastName" )
       public String getLastName() {
      
       return LastName;
       }
      
       @Column( name="Phone" )
       public String getPhone() {
      
       return Phone;
       }
      
       public void setUsername( String username ) {
      
       Username = username;
       }
      
       public void setEMail( String mail ) {
      
       EMail = mail;
       }
      
       public void setFirstName( String firstName ) {
      
       FirstName = firstName;
       }
      
       public void setLastName( String lastName ) {
      
       LastName = lastName;
       }
      
       public void setPhone( String phone ) {
      
       Phone = phone;
       }
      }
      


      MainView.java
       Session session = null;
      
       try{
      
       //SessionFactory sf = new Configuration().configure().buildSessionFactory();
       session = HibernateUtil.getSession();
      
       User usr = new User();
       usr.setUsername( "thetikigod" );
       usr.setFirstName( "tiki" );
       usr.setLastName( "god" );
       usr.setPhone( "9377668982" );
       usr.setEMail( "spamme@now.com" );
      
       session.beginTransaction();
       session.save(usr);
       session.getTransaction().commit();
       }
       catch(NoClassDefFoundError ncdfe){
      
       log.error( "Error" + ncdfe.getMessage(), ncdfe );
       notification.addError( "No class definition found: " + ncdfe.getMessage() );
       }
       catch( ExceptionInInitializerError eiie) {
      
       log.error( "Error" + eiie.getMessage(), eiie );
       notification.addError( eiie.getMessage() );
       }
       catch(Exception e) {
      
       log.error( "Error" + e.getMessage(), e );
       notification.addError( e.getMessage() );
       }
       finally {
      
       // Actual contact insertion will happen at this step
       try {
       session.flush();
       session.close();
       }
       catch (Exception e) {
      
       System.out.println(e.getMessage());
       }
       }
      


      Here is the exception I am getting:
      16:37:25,863 ERROR [MainView] Errornull
      java.lang.ExceptionInInitializerError
       at com.burris.ssapp.database.HibernateUtil.<clinit>(HibernateUtil.java:26)
       at com.burris.ssapp.user.views.MainView.prepare(MainView.java:333)
       at com.burris.ssapp.user.portlet.UserPortlet.doView(UserPortlet.java:80)
       at javax.portlet.GenericPortlet.doDispatch(GenericPortlet.java:133)
       at javax.portlet.GenericPortlet.render(GenericPortlet.java:306)
       at org.jboss.portal.portlet.impl.jsr168.PortletContainerImpl.invokeRender(PortletContainerImpl.java:483)
       at org.jboss.portal.portlet.impl.jsr168.PortletContainerImpl.dispatch(PortletContainerImpl.java:405)
       at org.jboss.portal.portlet.container.PortletContainerInvoker$1.invoke(PortletContainerInvoker.java:86)
       at org.jboss.portal.common.invocation.Invocation.invokeNext(Invocation.java:131)
       at org.jboss.portal.core.aspects.portlet.TransactionInterceptor.org$jboss$portal$core$aspects$portlet$TransactionInterceptor$invokeNotSupported$aop(TransactionInterceptor.java:86)
       at org.jboss.portal.core.aspects.portlet.TransactionInterceptor$invokeNotSupported_N4547270787964792031.invokeNext(TransactionInterceptor$invokeNotSupported_N4547270787964792031.java)
       at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
       at org.jboss.aspects.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:112)
       at org.jboss.portal.core.aspects.portlet.TransactionInterceptor$invokeNotSupported_N4547270787964792031.invokeNext(TransactionInterceptor$invokeNotSupported_N4547270787964792031.java)
       at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
       at org.jboss.aspects.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:102)
      


        • 1. Re: Trying to get JBoss and Hibernate to integrate
          jaikiran pai Master

          For debugging, why not just print the stacktrace in the catch block of your hibernate util:

          try {
          
           AnnotationConfiguration config = new AnnotationConfiguration();
           config.addAnnotatedClass( User.class );
           config.configure();
          
           sessionFactory = config.buildSessionFactory();
          
           } catch (Throwable ex) {
           ex.printStackTrace();
           // Log exception!
           throw new ExceptionInInitializerError(ex);
           }


          That might show you the actual error.

          • 2. Re: Trying to get JBoss and Hibernate to integrate
            Matt Cook Newbie

            I have tried just printing the stack trace there, however, the interpreter doesnt allow it because it is a static block that init's a constant. Ill try instantiating a sessionfactory outside of the hibernate util to see if I can see what the error is.

            Thank you for the quick reply.

            • 3. Re: Trying to get JBoss and Hibernate to integrate
              Matt Cook Newbie

              I added the session factory directly to the MainView class, and here are the exceptions that are produced:

              08:33:09,545 ERROR [MainView] Errornull
              java.lang.ExceptionInInitializerError
               at com.burris.ssapp.database.HibernateUtil.<clinit>(HibernateUtil.java:26)
               at com.burris.ssapp.user.views.MainView.prepare(MainView.java:333)
               at com.burris.ssapp.user.portlet.UserPortlet.doView(UserPortlet.java:80)
               at javax.portlet.GenericPortlet.doDispatch(GenericPortlet.java:133)
               at javax.portlet.GenericPortlet.render(GenericPortlet.java:306)
              
              ...
              
              Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
               at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
               at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
               at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
               at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
               at com.burris.ssapp.database.HibernateUtil.<clinit>(HibernateUtil.java:19)
              


              08:33:13,970 ERROR [MainView] Errornull
              java.lang.NoClassDefFoundError
               at com.burris.ssapp.user.views.MainView.prepare(MainView.java:333)
               at com.burris.ssapp.user.portlet.UserPortlet.doView(UserPortlet.java:80)
               at javax.portlet.GenericPortlet.doDispatch(GenericPortlet.java:133)
               at javax.portlet.GenericPortlet.render(GenericPortlet.java:306)
               at org.jboss.portal.portlet.impl.jsr168.PortletContainerImpl.invokeRender(PortletContainerImpl.java:483)
               at org.jboss.portal.portlet.impl.jsr168.PortletContainerImpl.dispatch(PortletContainerImpl.java:405)
              


              • 4. Re: Trying to get JBoss and Hibernate to integrate
                jaikiran pai Master

                The first error says that the hibernate.cfg.xml is not found in the classpath. You mention that you placed this file in the META-INF folder. Instead, move it one level up to the root of the project.

                The second error appears to be because of some missing jar file in the classpath. Not sure which one, because its not printing the classname.

                • 5. Re: Trying to get JBoss and Hibernate to integrate
                  Matt Cook Newbie

                  I tried moving the hibernate.cfg.xml to the root directory and I get the stack trace:

                  Cause: org.hibernate.HibernateException: /hibernate.cfg.xml not found
                  Message: /hibernate.cfg.xml not found
                  StackTrace:
                  
                  org.hibernate.HibernateException: /hibernate.cfg.xml not found
                   at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
                   at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
                   at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
                   at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
                   at com.burris.ssapp.user.views.MainView.prepare(MainView.java:330)
                   at com.burris.ssapp.user.portlet.UserPortlet.doView(UserPortlet.java:80)
                   at javax.portlet.GenericPortlet.doDispatch(GenericPortlet.java:133)
                   at javax.portlet.GenericPortlet.render(GenericPortlet.java:306)
                   at org.jboss.portal.portlet.impl.jsr168.PortletContainerImpl.invokeRender(PortletContainerImpl.java:483)
                   at org.jboss.portal.portlet.impl.jsr168.PortletContainerImpl.dispatch(PortletContainerImpl.java:405)
                   at org.jboss.portal.portlet.container.PortletContainerInvoker$1.invoke(PortletContainerInvoker.java:86)
                   at org.jboss.portal.common.invocation.Invocation.invokeNext(Invocation.java:131)
                   at org.jboss.portal.core.aspects.portlet.TransactionInterceptor.org$jboss$portal$core$aspects$portlet$TransactionInterceptor$invokeNotSupported$aop(TransactionInterceptor.java:86)
                   at org.jboss.portal.core.aspects.portlet.TransactionInterceptor$invokeNotSupported_N4547270787964792031.invokeNext(TransactionInterceptor$invokeNotSupported_N4547270787964792031.java)
                   at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
                   at org.jboss.aspects.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:112)
                   at org.jboss.portal.core.aspects.portlet.TransactionInterceptor$invokeNotSupported_N4547270787964792031.invokeNext(TransactionInterceptor$invokeNotSupported_N4547270787964792031.java)
                   at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
                   at org.jboss.aspects.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:102)
                   at org.jboss.portal.core.aspects.portlet.TransactionInterceptor$invokeNotSupported_N4547270787964792031.invokeNext(TransactionInterceptor$invokeNotSupported_N4547270787964792031.java)
                   at org.jboss.portal.core.aspects.portlet.TransactionInterceptor.invokeNotSupported(TransactionInterceptor.java)
                   at org.jboss.portal.core.aspects.portlet.TransactionInterceptor.invoke(TransactionInterceptor.java:56)
                   at org.jboss.portal.portlet.invocation.PortletInterceptor.invoke(PortletInterceptor.java:38)
                   at org.jboss.portal.common.invocation.Invocation.invokeNext(Invocation.java:115)
                   at org.jboss.portal.core.aspects.portlet.HeaderInterceptor.invoke(HeaderInterceptor.java:50)
                   at org.jboss.portal.portlet.invocation.PortletInterceptor.invoke(PortletInterceptor.java:38)
                   at org.jboss.portal.common.invocation.Invocation.invokeNext(Invocation.java:115)
                   at org.jboss.portal.portlet.aspects.portlet.ProducerCacheInterceptor.invoke(ProducerCacheInterceptor.java:58)
                   at org.jboss.portal.portlet.invocation.PortletInterceptor.invoke(PortletInterceptor.java:38)
                   at org.jboss.portal.common.invocation.Invocation.invokeNext(Invocation.java:115)
                   at org.jboss.portal.core.aspects.portlet.AjaxInterceptor.invoke(AjaxInterceptor.java:51)
                   at org.jboss.portal.portlet.invocation.PortletInterceptor.invoke(PortletInterceptor.java:38)
                   at org.jboss.portal.common.invocation.Invocation.invokeNext(Invocation.java:115)
                   at org.jboss.portal.portlet.aspects.portlet.ModesInterceptor.invoke(ModesInterceptor.java:62)
                   at org.jboss.portal.portlet.invocation.PortletInterceptor.invoke(PortletInterceptor.java:38)
                   at org.jboss.portal.common.invocation.Invocation.invokeNext(Invocation.java:115)
                   at org.jboss.portal.portlet.bridge.BridgeInterceptor.invoke(BridgeInterceptor.java:47)
                   at org.jboss.portal.portlet.invocation.PortletInterceptor.invoke(PortletInterceptor.java:38)
                  


                  Before I moved the cfg to the root level, I got these messages in the console a few seconds after deploy:

                  10:18:57,867 INFO [Configuration] configuring from resource: /hibernate.cfg.xml
                  10:18:57,867 INFO [Configuration] Configuration resource: /hibernate.cfg.xml
                  


                  This leads me to believe it was finding it in the META-INF folder.

                  http://www.hibernate.org/66.html
                  As far as the jars go, the above site lists the necessary files to be in the server lib directory. They are all there except

                  commons-lang.jar
                  jcs.jar
                  odmg.jar

                  I also have not included these in the project. I have only included the files to keep the compiler from complaining at me. I will locate these jars and place them in the lib directory and also try including them in the project. I am not sure if these could be related to my problem.

                  • 6. Re: Trying to get JBoss and Hibernate to integrate
                    Matt Cook Newbie

                    It looks like not all those jars are there in the hibernate lib folder... after looking it over that tutorial looks like it is for hibernate 2, so I wonder if that is why they arent in the directory. I am still having issues with the hibernate.cfg.xml. It is now not being recognized in the meta-inf folder nor in the root directory. This has really left me stumped.

                    • 7. Re: Trying to get JBoss and Hibernate to integrate
                      Matt Cook Newbie

                      Hey all thanks for the help. I just got it working. I moved the hibernate.cfg.xml file to the WEB-INF folder and changed the xml a bit. Heres the new config for those who want to see it:

                      <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                       "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
                      
                      <hibernate-configuration>
                       <session-factory>
                       <!-- Database connection settings -->
                       <property name="connection.datasource">java:/SupplySideDS</property>
                      
                       <!-- SQL dialect -->
                       <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
                      
                       <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
                       <property name="current_session_context_class">thread</property>
                      
                       <!-- Echo all executed SQL to stdout -->
                       <property name="show_sql">true</property>
                       </session-factory>
                      </hibernate-configuration>
                      


                      After this all the exceptions went away. YAY. Thanks again for all the help.