Project

General

Profile

Sample-springds-hibernate » History » Revision 8

Revision 7 (Henning Blohm, 17.10.2015 20:24) → Revision 8/13 (Henning Blohm, 06.05.2018 17:42)

h1. A sample using Spring data sources and Spring transaction management in a modular application 

 This sample is very similar to [[Sample-spring-hibernate]] but differs in that we use 

 * Spring configured data sources 
 * A Spring transaction manager (in contrast to Atomikos as in [[Sample-jta-spring]] or the built-in TM as in [[Sample-spring-hibernate]]). 

 This is another practical application of [[How to Spring]]. 

 This sample is stored in "z2-samples.springds-hibernate":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-springds-hibernate. 

 h2. Prerequisites 

 {{include(Java Version Requirements)}} 

 You need to run Java DB as network server on localhost. This is explained next. 

 The application will create a database "z2-samples" 

 {{include(How to run Java db)}} 

 h2. Run it 

 Like all samples, also this sample can be run as in [[How to run a sample]]. If you have the database, the fastest way to verify whether it runs is: 

 <pre><code class="ruby"> 
 mkdir install 
 cd install  
 git clone -b v2.5 master http://git.z2-environment.net/z2-base.core 
 git clone -b v2.5 master http://git.z2-environment.net/z2-samples.springds-hibernate 

 # on Linux / Mac OS: 
 cd z2-base.core/run/bin 
 ./gui.sh 

 # on Windows: 
 cd z2-base.core\run\bin 
 gui.bat 
 </code></pre> 

 When running, go to http://localhost:8080/springds-hibernate. You should see this: 

 !{width:953px}springds-hibernate.png! 

 h2. Details 

 Similar to [[Sample-spring-hibernate]], the assumption of this example is that of a re-use domain module *com.zfabrik.samples.springds-hibernate.domain* that implements a "Thingy Repository" and is used from a web application that is in another module *com.zfabrik.samples.springds-hibernate.web*.  
 The domain module declares the date source used as well as the transaction manager. The transaction manager is exposed as Z2 component. 

 The body of the application context ("here":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-springds-hibernate/revisions/master/entry/com.zfabrik.samples.springds-hibernate.domain/java/src.impl/META-INF/applicationContext.xml) of the domain module looks like this: 

 <pre><code class="xml"> 
	 <!-- annotation based config --> 
	 <context:component-scan base-package="com.zfabrik.samples.spring_hibernate" /> 
	 <context:annotation-config /> 

	 <!-- DB config (no pooling here - but can be changed easily) --> 
	 <!--    Do not use DriverManagerDataSource here for class loading considerations --> 

	 <bean id="dataSource" class="org.apache.derby.jdbc.ClientDataSource"> 
		 <property name="databaseName" value="z2-samples"/> 
		 <property name="serverName" value="localhost"/> 
		 <property name="portNumber" value="1527"/> 
		 <property name="createDatabase" value="create"/> 
	 </bean> 

	 <!-- The actual EMF we use --> 
	 <bean id="entityManagerFactory" 	 class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
		 <property name="dataSource" ref="dataSource" /> 
		 <property name="persistenceUnitName" value="thingies" /> 
		 <property name="jpaVendorAdapter"> 
			 <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
				 <property name="showSql" value="true" /> 
				 <property name="generateDdl" value="true" /> 
				 <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" /> 
			 </bean> 
		 </property> 
	 </bean> 

	 <!--    the transaction manager --> 
	 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
	    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
	 </bean> 

	 <!-- EntityManager injection --> 
	 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
 </code></pre> 

 The persistence unit "thingies", defined in the same module (as in "java/src.impl/META-INF/applicationContext.xml":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-springds-hibernate/revisions/master/entry/com.zfabrik.samples.springds-hibernate.domain/java/src.impl/META-INF/persistence.xml), is essentially empty must be thought as completed by the application context declaration above (which is a Spring feature). 

 The domain module exposes the Thingy Repository as a Z2 component - from a Spring application context defined bean -    that is imported into the application context of the Web application and injected into the "controller filter":https://redmine.z2-environment.net/projects/z2-environment/wiki/Sample-springds-hibernate/edit by Spring.  

 The controller filter uses declarative transaction demarcation relying on the transaction manager exposed by the domain module (note the "transactionManager" bean declaration below). 

 Here is the body of the Web apps application context (as declared in "WEB-INF/applicationContext.xml":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-springds-hibernate/revisions/master/entry/com.zfabrik.samples.springds-hibernate.web/web/WebContent/WEB-INF/applicationContext.xml) 

 <pre><code class="xml"> 
	 <!-- Annotation Support --> 
	 <context:component-scan base-package="com.zfabrik.samples.spring_hibernate" /> 
	 <context:spring-configured /> 
	 <context:annotation-config /> 		

	 <!-- import transaction manager from domain.     --> 
     <bean id="transactionManager" class="com.zfabrik.springframework.ComponentFactoryBean"> 
         <property name="componentName"    value="com.zfabrik.samples.springds-hibernate.domain/transactionManager" /> 
         <property name="className"    value="org.springframework.transaction.support.AbstractPlatformTransactionManager" /> 
     </bean> 

	 <!-- make sure we can use @Transactional with the Spring aspect --> 
	 <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/> 

	 <!-- import external services --> 
     <bean id="thingyRepository" class="com.zfabrik.springframework.ComponentFactoryBean"> 
         <property name="componentName"    value="com.zfabrik.samples.springds-hibernate.domain/repository" /> 
         <property name="className"    value="com.zfabrik.samples.spring_hibernate.thingies.ThingyRepository" /> 
     </bean> 
 </code></pre>