Seam and Hibernate

Seam provides extensive support for Hibernate. If you decide to use Tapestry and Seam together, you dont need Tapestry/Hibernate Integration Library provided by Tapestry.

Using a Seam-managed Hibernate session

First you have to configure Hibernate. One way to do it is to create a hibernate.cfg.xml. Secondly, you can configure Seam to create Hibernate Session by writing following into your components.xml:

<components xmlns="http://jboss.com/products/seam/components"
        xmlns:core="http://jboss.com/products/seam/core"
        xmlns:persistence="http://jboss.com/products/seam/persistence"
        xmlns:security="http://jboss.com/products/seam/security"
        xmlns:transaction="http://jboss.com/products/seam/transaction"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=
            "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.0.xsd 
             http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.0.xsd
             http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.0.xsd
             http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.0.xsd
             http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd">
    
    <persistence:hibernate-session-factory name="hibernateSessionFactory"/>
    
    <persistence:managed-hibernate-session name="userDatabase"
                                session-factory="#{hibernateSessionFactory}"
                                auto-create="true"/>
</components>

This configuration creates a conversation-scoped Seam component named userDatabase. The managed Hibernate Session can be injected into your JavaBean components using the following code

@Name("userService")
public class UserService {
    
    @In
    private Session userDatabase;
    
    public List<User> findAll(){
        return userDatabase.createCriteria(User.class).list()
    }
}

The component UserService can be injected into your Tapestry pages:

@In(create=true) private UserService userService;

Limitations

So far it is not possible to inject a Seam-managed Hibernate session directly into your Tapestry pages or components. You need to inject the session into a Seam component which can be injected in your pages or components.