Project

General

Profile

Sample-springds-hibernate » History » Version 5

Henning Blohm, 10.09.2015 15:28

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