Monolithic configuration
ti-hibernate provides a simple way to distribute the mapping resources and mapping classes needed to be registered into the session factory configuration.
A simple hibernate session factory configuration (example taken from the spring reference documentation) is shown below:
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="mappingResources"> <list> <value>product.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.HSQLDialect </value> </property> </bean>
Distributed configuration
To distribute the configuration above, use the following configuration snippets.
Distribution of mapping resources:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tiCore="http://antidoto.sf.net/subsystem/ti-core" xmlns:tiHibernate="http://antidoto.sf.net/subsystem/ti-hibernate" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://antidoto.sf.net/subsystem/ti-core http://antidoto.sf.net/xsd/ti-core.xsd http://antidoto.sf.net/subsystem/ti-hibernate http://antidoto.sf.net/xsd/ti-hibernate.xsd "> <tiHibernate:mappings dbalias="default"> <tiHibernate:mapping resource="product.hbm.xml" /> </tiHibernate:mappings> </beans>
Distribution of properties:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tiCore="http://antidoto.sf.net/subsystem/ti-core" xmlns:tiHibernate="http://antidoto.sf.net/subsystem/ti-hibernate" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://antidoto.sf.net/subsystem/ti-core http://antidoto.sf.net/xsd/ti-core.xsd http://antidoto.sf.net/subsystem/ti-hibernate http://antidoto.sf.net/xsd/ti-hibernate.xsd "> <tiHibernate:properties dbalias="default"> <tiHibernate:prop key="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" /> </tiHibernate:properties> </beans>
The mapping resources and properties must be labeled with a dbalias. Through this label it is possible to have multiple session factory configurations and have the resources and properties assigned to the right sessiong factory configuration
For every dbalias configured there must be a corresponding session factory configuration
ti-hibernate provides a delegating local session factory beans that collects the configuration fragments. This session factory bean must be used in conjunction with the configuration snippets above. The constructor argument defines the dbalias assigned to this session factory.
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tiCore="http://antidoto.sf.net/subsystem/ti-core" xmlns:tiHibernate="http://antidoto.sf.net/subsystem/ti-hibernate" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://antidoto.sf.net/subsystem/ti-core http://antidoto.sf.net/xsd/ti-core.xsd http://antidoto.sf.net/subsystem/ti-hibernate http://antidoto.sf.net/xsd/ti-hibernate.xsd "> <bean id="sessionFactory" class="de.pgt.ti.hibernate.context.DelegatingLocalSessionFactoryBean" lazy-init="false"> <constructor-arg> <value>default</value> </constructor-arg> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> </beans>