Configuring the Hibernate Session

There are three ways to retrieve a Hibernate Session. If your application runs within a managed environment like an application server, there is an internal Hibernate mechanism to retrieve a session. If the application is not running within an application server but stand-alone, Infinity Process Platform provides an own mechanism to retrieve a Hibernate session. At least it is also possible to run the application within a Spring-managed environment. In this case Spring supports a mechanism to retrieve a session. In all these cases you have to configure your application in a specific way that allows Infinity Process Platform to have the right look up for the Hibernate session.

Hibernate Session Factory

Infinity Process Platform retrieves the Hibernate session by using the currentSession method of the Hibernate's SessionFactory class. Precondition is that the application runs within an application server and you have defined the Hibernate configuration file like following exemplary one:

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.cglib.use_reflection_optimizer">true</property>
        <property name="connection.provider_class">org.hibernate.connection.DatasourceConnectionProvider</property>
        <property name="connection.datasource">java:/sampleDB</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
        <property name="transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
        <property name="dialect">org.hibernate.dialect.DerbyDialect</property>
    </session-factory>
</hibernate-configuration>
	

This configuration file is also the place for your Hibernate object mappings. Please refer to the Hibernate documentation on www.hibernate.org for further information.

Spring Session Factory

If Infinity Process Platform runs within a Spring-managed environment, it retrieves the Hibernate session through the Spring application context. In this case you should enhance the carnot-spring-context.xml with the following bean definition or add this definition to a custom context file:

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource">
		<ref bean="carnotXaAuditTrailDataSource"></ref>
	</property>
	<property name="jtaTransactionManager">
		<bean factory-bean="jtaTxManager" factory-method="getTransactionManager"></bean>
	</property>
	<property name="mappingLocations">
		<value>classpath:${MappingDirectoryLocations}</value>
	</property>
	<property name="hibernateProperties" ref="hibernatePropertiesBean"></property>
</bean>

For further information on how to configure Infinity Process Platform to run within a Spring managed environment, please refer to the Spring Integration Guide.

Using Hibernate in RAD Environment

To use Hibernate in RAD-environment, add the following code to carnot-spring-context.xml in the WebContent/WEB-INF/config/ipp/spring folder of the RAD Web project. Note that mappingLocations-entry has to be edited to point to the correct location of the mapping files.

<bean id="hibernatePropertiesBean"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean"
    lazy-init="true">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.DerbyDialect
            </prop>
            <prop key="hibernate.connection.autocommit">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="hibernateSessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
    lazy-init="true">
    <property name="dataSource">
        <ref bean="carnotXaAuditTrailDataSource" />
    </property>
    <property name="jtaTransactionManager">
        <bean factory-bean="jtaTxManager"
            factory-method="getTransactionManager" />
    </property>
    <property name="mappingLocations">
        <value>classpath*:**/*.hbm.xml</value>
    </property>
    <property name="hibernateProperties"
        ref="hibernatePropertiesBean" />
</bean>