Project

General

Profile

Sample-springds-hibernate » History » Version 3

Henning Blohm, 03.05.2014 18:52

1 2 Henning Blohm
h1. A sample using Spring data sources and Spring transaction management in a modular application
2 1 Henning Blohm
3 2 Henning Blohm
This sample is very similar to [[Sample-spring-hibernate]] but differs in that we use
4
5
* Spring configured data sources
6
* A Spring transaction manager (in contrast to Atomikos as in [[Sample-jta-spring]] or the built-in TM as in [[Sample-spring-hibernate]].
7
8
This is another practical application of [[How to Spring]].
9
10
This sample is stored in "z2-samples.springds-hibernate":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-springds-hibernate.
11
12
h2. Prerequisites
13
14
You need to run Java DB as network server on localhost. This is explained next.
15
16
The application will create a database "z2-samples"
17
18
{{include(How to run Java db)}}
19
20
h2. Run it
21
22
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:
23
24
<pre><code class="ruby">
25
mkdir install
26
cd install 
27
git clone -b master http://git.z2-environment.net/z2-base.core
28
git clone -b master http://git.z2-environment.net/z2-samples.springds-hibernate
29
30
# on Linux / Mac OS:
31
cd z2-base.core/run/bin
32
./gui.sh
33
34
# on Windows:
35
cd z2-base.core\run\bin
36
gui.bat
37
</code></pre>
38
39
When running, go to http://localhost:8080/springds-hibernate. You should see this:
40
41 3 Henning Blohm
!{width:953px}springds-hibernate.png!
42
43
h2. Details
44
45
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*. 
46
The domain module declares the date source used as well as the transaction manager. The transaction manager is exposed as Z2 component.
47
48
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:
49
50
<pre><code class="xml">
51
	<!-- annotation based config -->
52
	<context:component-scan base-package="com.zfabrik.samples.spring_hibernate" />
53
	<context:annotation-config />
54
55
	<!-- DB config (no pooling here - but can be changed easily) -->
56
	<!--  Do not use DriverManagerDataSource here for class loading considerations -->
57
58
	<bean id="dataSource" class="org.apache.derby.jdbc.ClientDataSource">
59
		<property name="databaseName" value="z2-samples"/>
60
		<property name="serverName" value="localhost"/>
61
		<property name="portNumber" value="1527"/>
62
		<property name="createDatabase" value="create"/>
63
	</bean>
64
65
	<!-- The actual EMF we use -->
66
	<bean id="entityManagerFactory"	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
67
		<property name="dataSource" ref="dataSource" />
68
		<property name="persistenceUnitName" value="thingies" />
69
		<property name="jpaVendorAdapter">
70
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
71
				<property name="showSql" value="true" />
72
				<property name="generateDdl" value="true" />
73
				<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
74
			</bean>
75
		</property>
76
	</bean>
77
78
	<!--  the transaction manager -->
79
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
80
	   <property name="entityManagerFactory" ref="entityManagerFactory" />
81
	</bean>
82
83
	<!-- EntityManager injection -->
84
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
85
</code></pre>
86
87
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).
88
89
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 by Spring. 
90
91
The controller uses declarative transaction demarcation. It uses the transaction manager exposed by the domain module so that transaction demarcation works over the same TM.
92
93
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)
94
95
<pre><code class="xml">
96
	<!-- Annotation Support -->
97
	<context:component-scan base-package="com.zfabrik.samples.spring_hibernate" />
98
	<context:spring-configured />
99
	<context:annotation-config />		
100
101
	<!-- import transaction manager from domain.   -->
102
    <bean id="transactionManager" class="com.zfabrik.springframework.ComponentFactoryBean">
103
        <property name="componentName"  value="com.zfabrik.samples.springds-hibernate.domain/transactionManager" />
104
        <property name="className"  value="org.springframework.transaction.support.AbstractPlatformTransactionManager" />
105
    </bean>
106
107
	<!-- make sure we can use @Transactional with the Spring aspect -->
108
	<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
109
110
	<!-- import external services -->
111
    <bean id="thingyRepository" class="com.zfabrik.springframework.ComponentFactoryBean">
112
        <property name="componentName"  value="com.zfabrik.samples.springds-hibernate.domain/repository" />
113
        <property name="className"  value="com.zfabrik.samples.spring_hibernate.thingies.ThingyRepository" />
114
    </bean>
115
</code></pre>