hibernate database connection
fuchs Nov 5, 2009 2:05 PMhello,
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?