1 Reply Latest reply: Oct 11, 2010 4:18 AM by Adrian Mitev RSS

    Inject Hibernate Session object in EJB

    Alex Serezhenko Newbie
      Hi every one.
      I have a problem.
      I cant inject Hibernate Session onject in Stateless EJB.
      I use Seam 2.2.0 and jBoss 5.1 AS.
      Here my configuration files.

      hibernate.cfg.xml
      `<hibernate-configuration>
      <session-factory name="java:comp:/hibernateSessionFactory">
        <property name="hibernate.connection.pool_size">10</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/seam</property>
        <property name="hibernate.connection.username">surly</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.default_entity_mode">pojo</property>
        <property name="hibernate.session_factory_name">java:comp:/hibernateSessionFactory</property>
        <property name="hibernate.connection.datasource">seam_testDatasource</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
        <property name="hibernate.transaction.auto_close_session">false</property>
        <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
        <property name="hibernate.transaction.flush_before_completion">true</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <!--  <mapping class="myentity" package="org.domain.seamtest.session"/> -->
      </session-factory>`

      persistance.xml
      '<persistence-unit name="seam_test" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/seam_testDatasource</jta-data-source>
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
               <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="myfactory"/>
            </properties>
         </persistence-unit>'

      components.xml
      '....
      <persistence:hibernate-session-factory auto-create="true"
        cfg-resource-name="hibernate.cfg.xml" name="hibernateSessionFactory"/>
      <persistence:managed-hibernate-session auto-create="true"
        name="hibernateSession" session-factory-jndi-name="hibernateSessionFactory"/>
      ....'

      seam_test-ds.xml
      '
      <datasources>
            <local-tx-datasource>
            <jndi-name>seam_testDatasource</jndi-name>
            <use-java-context>true</use-java-context>
            <connection-url>jdbc:mysql://localhost:3306/seam</connection-url>
            <driver-class>com.mysql.jdbc.Driver</driver-class>
            <user-name>surly</user-name>
            <password></password>
            </local-tx-datasource>
      </datasources>
      '
      mysession.java, base class DAO.java, local interface
      'public class DAO {
         protected DAO() {
            }
            //some methods

            private static final Logger log = Logger.getAnonymousLogger();     
                    //HERE IS IT!!
            @In
            public Session hibernateSession;
      }


      @Name("mysession")
      public class mysession extends DAO implements mysessionLocal {
         @PostConstruct
         public void init() {
                   System.out.println("INIT mysession bean");
          }
                 //some methods
      }
      @Local
      public interface mysessionLocal {
                //methods
      }'


      I write simple web-application using SEAM framework, and i decided to use Hibernate (not JPA) in my stateless EJB.
      I want to INJECT (@In) Hibernate Session in every created EJB.
      i don't want to use EntitiyManager object, which injected by using @PersistenceContext annotation.
      Tell me, what is wrong here, why i cant to inject Session to EJB?
      i'm always getting this, when my EJB constructed:

      '00:31:10,091 ERROR [TxPolicy] javax.ejb.EJBTransactionRolledbackException: SessionFactory not found in JNDI: hibernateSessionFactory'
        • 1. Re: Inject Hibernate Session object in EJB
          Adrian Mitev Master

          I think you're doing it wrong. Remove


          <persistence:hibernate-session-factory auto-create="true"
            cfg-resource-name="hibernate.cfg.xml" name="hibernateSessionFactory"/>
          



          and set the jndi to java:/EMS21EntityManagerFactory like this:


          <persistence:managed-hibernate-session auto-create="true"
            name="hibernateSession" session-factory-jndi-name="java:/EMS21EntityManagerFactory"/>