Project

General

Profile

Sample-springds-hibernate » History » Version 8

Henning Blohm, 06.05.2018 17:42

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