JBoss.org Community Documentation

Chapter 35. Injecting Hibernate Session and Session Factory in JBoss EJB3

Persistent classes that are mapped using Hibernate *.hbm.xml files are supported in JBoss. The EJB3 Deployer will search the archive for any .hbm.xml files and add them to the definition of the underlying Hibernate SessionFactory. These .hbm.xml files can be virtually anywhere within the archive under any java package or directory. Take a look at the customer.hbm.xml for an example.

Class mappings defined in .hbm.xml files can be managed by EntityManagers just as annotated @Entity beans are. Also, you are allowed to have relationships between a .hbm.xml mapped class and an EJB3 entity. So, mixing/matching is allowed. Which means you can have some entities defined in .hbm.xml and some others through @Entity annotations.

Injecting Hibernate Session and SessionFactory :

You can inject a org.hibernate.Session and org.hibernate.SessionFactory directly into your EJBs just as you can do with EntityManagers and EntityManagerFactorys. The behavior of a Session is just the same as the behavior of an injected EntityManager. The application server controls the lifecycle of the Session so that you do not have to open, flush, or close the session. Extended persistence contexts also work with injected Hibernate Sessions.

         	
import org.hibernate.Query;
import org.hibernate.Session;

@Stateless
@Remote(CustomerRemote.class)
@RemoteBinding(jndiBinding = "CustBean")
public class CustomerBean implements CustomerRemote
{

   @PersistenceContext
   private Session session;

         	
         

Take a look at org.jboss.tutorial.hibernate.bean.CustomerBean for more details.

Accessing org.hibernate.Session and org.hibernate.Query from EntityManager:

You can get access to the current underlying Hibernate Session by calling the getDelegate method on the EntityManager :

      		
@Stateless
@Remote(CustomerRemote.class)
@RemoteBinding (jndiBinding="AnotherCustBean")
public class AnotherCustomerBean implements CustomerRemote
{

   @PersistenceContext
   private EntityManager em;

   public Customer getCustomer(long id)
   {
      org.hibernate.Session session = (Session) em.getDelegate();
      return (Customer) session.get(Customer.class, id);
   }
...
			
      	

Take a look at org.jboss.tutorial.hibernate.bean.AnotherCustomerBean for more details.

You can get access to the current underlying Hibernate Query by typecasting your reference to a org.hibernate.ejb.QueryImpl.

      		
public List<Customer> getCustomers(String fname)
{
   org.hibernate.ejb.QueryImpl queryImpl = (QueryImpl) em.createQuery("from Customer where fname ='" + fname + "'");
   org.hibernate.Query query = queryImpl.getHibernateQuery();
   return query.list();
}

			
      

Building and Running

From the command prompt, move to the "hibernate" folder under the Section 1.3, “Set the EJB3_TUTORIAL_HOME”

Ant Users:

Make sure your JBossAS-5.x is running

	
$ ant
$ ant run

run:
     [java] Jai Pai created with id = 1
     [java] Jaikiran Pai created with id = 2
     [java] Jai NoLastName created with id = 3
     [java] Searching for customer with id = 2
     [java] Found customer Jaikiran Pai with id = 2
     [java] Searching for customer with id = 3
     [java] Found customer Jai NoLastName with id = 3
     [java] Searching for customers with first name Jai
     [java] Found 2 customers with first name Jai
     [java] Searching for customers with first name Jaikiran
     [java] Found 1 customers with first name Jaikiran

     
	
Maven Users: Make sure the AS is not running.
$ mvn clean install -PRunSingleTutorial