1 Reply Latest reply on Nov 9, 2009 8:54 AM by Gabriel Muench

    hibernate database connection

    Gabriel Muench Newbie

      hello,

      iam using jboss portal 2.7.2 bundle.
      i like to use my own mysql database with hibernate functionality which is called from some portlets.

      But i get this error:
      19:48:07,463 ERROR [*Home] Could not locate SessionFactory in JNDI
      javax.naming.NameNotFoundException: SessionFactory not bound

      i made a java utility project:
      MyProject
      -package.tables (*.Home.class/*.class/*.hbm.xml)
      -META-INF (hibernate-service.xml)
      -hibernate.cfg.xml

      hibernate.cfg.xml:

      <hibernate-configuration>
       <session-factory>
       <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
       <property name="hibernate.connection.datasource">java:jdbc/CustomerDB</property>
       <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
       <property name="current_session_context_class">thread</property>
       <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
      
       <mapping resource="package/tables/*Home.hbm.xml" />
       <mapping resource="package/tables/*.hbm.xml" />
      
       </session-factory>
       </hibernate-configuration>



      hibernate-ds.xml
      <datasources>
       <local-tx-datasource>
       <jndi-name>jdbc/CustomerDB</jndi-name>
       <connection-url>jdbc:mysql://mysqlserver/mydataschema</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class>
       <user-name>****</user-name>
       <password>****</password>
       <min-pool-size>5</min-pool-size>
       <max-pool-size>20</max-pool-size>
       </local-tx-datasource>
      </datasources>


      hibernate-service.xml
      <server>
       <mbean code="org.jboss.portal.jems.hibernate.SessionFactoryBinder" name="jboss.har:service=Hibernate">
       <attribute name="DatasourceName">java:/CustomerDB</attribute>
       <attribute name="Dialect">org.hibernate.dialect.MySQLDialect</attribute>
       <attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute>
       <attribute name="CacheProviderClass">org.hibernate.cache.NoCacheProvider</attribute>
       </mbean>
      </server>


      The code which looks up for the jndi:
       private final SessionFactory sessionFactory = getSessionFactory();
      
       protected SessionFactory getSessionFactory() {
       try {
       return (SessionFactory) new InitialContext()
       .lookup("java:/hibernate/SessionFactory");
       } catch (Exception e) {
       log.error("Could not locate SessionFactory in JNDI", e);
       throw new IllegalStateException(
       "Could not locate SessionFactory in JNDI");
       }
       }
      



      I read a lot of post, but never found a solution.
      Can i get some help, please?



        • 1. Re: hibernate database connection
          Gabriel Muench Newbie

          The mappings and classes together with the hibernate-service.xml will be packaged into a HAR-File in the deploy-archiv of jboss.

          But for the JNDI-binding i need a hibernate configuration file.
          In the Book "JBoss in Action" this file is called XX-hibernate.xml. Hibernate is calling this file hibernate.cfg.xml or hibernate.properties.

          I think, this should be placed in the jboss-deploy-archiv. But i dont know where?

          Has someone an idea?
          Thanks for helping!

          kind regards.