10.3.2. Using a Seam-managed Hibernate session
Seam-managed Hibernate sessions work in a similar fashion. In
components.xml
:
<persistence:hibernate-session-factory name="hibernateSessionFactory"/> <persistence:managed-hibernate-session name="bookingDatabase" auto-create="true" session-factory-jndi-name="java:/bookingSessionFactory"/>
Here,
java:/bookingSessionFactory
is the name of the session factory specified in hibernate.cfg.xml
.
<session-factory name="java:/bookingSessionFactory"> <property name="transaction.flush_before_completion">true</property> <property name="connection.release_mode">after_statement</property> <property name="transaction.manager_lookup_class"> org.hibernate.transaction.JBossTransactionManagerLookup </property> <property name="transaction.factory_class"> org.hibernate.transaction.JTATransactionFactory </property> <property name="connection.datasource"> java:/bookingDatasource </property> ... </session-factory>
Note
Seam does not synchronize the session with the database, so always enable
hibernate.transaction.flush_before_completion
to ensure that the session is automatically synchronized before the JTA transaction commits.
We can now inject a managed Hibernate
Session
into our JavaBean components with the following code:
@In Session bookingDatabase;