Project

General

Profile

Sample-springds-hibernate » History » Version 12

Henning Blohm, 07.09.2021 19:11

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 9 Henning Blohm
If you have the database, the fastest way to verify whether it runs is:
25 1 Henning Blohm
26 9 Henning Blohm
{{include(Install_sample_prefix)}}
27 2 Henning Blohm
28 9 Henning Blohm
Check out the sample
29 2 Henning Blohm
30 10 Henning Blohm
<pre><code class="bash">
31 12 Henning Blohm
git clone -b v2.9 https://www.z2-environment.net/git/z2-samples.springds-hibernate
32 2 Henning Blohm
</code></pre>
33 9 Henning Blohm
34
{{include(Install_sample_postfix)}}
35 2 Henning Blohm
36
When running, go to http://localhost:8080/springds-hibernate. You should see this:
37
38 3 Henning Blohm
!{width:953px}springds-hibernate.png!
39
40
h2. Details
41
42
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*. 
43
The domain module declares the date source used as well as the transaction manager. The transaction manager is exposed as Z2 component.
44
45
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:
46
47
<pre><code class="xml">
48
	<!-- annotation based config -->
49
	<context:component-scan base-package="com.zfabrik.samples.spring_hibernate" />
50
	<context:annotation-config />
51
52
	<!-- DB config (no pooling here - but can be changed easily) -->
53
	<!--  Do not use DriverManagerDataSource here for class loading considerations -->
54
55
	<bean id="dataSource" class="org.apache.derby.jdbc.ClientDataSource">
56
		<property name="databaseName" value="z2-samples"/>
57
		<property name="serverName" value="localhost"/>
58
		<property name="portNumber" value="1527"/>
59
		<property name="createDatabase" value="create"/>
60
	</bean>
61
62
	<!-- The actual EMF we use -->
63
	<bean id="entityManagerFactory"	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
64
		<property name="dataSource" ref="dataSource" />
65
		<property name="persistenceUnitName" value="thingies" />
66
		<property name="jpaVendorAdapter">
67
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
68
				<property name="showSql" value="true" />
69
				<property name="generateDdl" value="true" />
70
				<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
71
			</bean>
72
		</property>
73
	</bean>
74
75
	<!--  the transaction manager -->
76
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
77
	   <property name="entityManagerFactory" ref="entityManagerFactory" />
78
	</bean>
79
80
	<!-- EntityManager injection -->
81
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
82
</code></pre>
83
84
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).
85
86 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. 
87 3 Henning Blohm
88 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).
89 3 Henning Blohm
90
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)
91
92
<pre><code class="xml">
93
	<!-- Annotation Support -->
94
	<context:component-scan base-package="com.zfabrik.samples.spring_hibernate" />
95
	<context:spring-configured />
96
	<context:annotation-config />		
97
98
	<!-- import transaction manager from domain.   -->
99
    <bean id="transactionManager" class="com.zfabrik.springframework.ComponentFactoryBean">
100
        <property name="componentName"  value="com.zfabrik.samples.springds-hibernate.domain/transactionManager" />
101
        <property name="className"  value="org.springframework.transaction.support.AbstractPlatformTransactionManager" />
102
    </bean>
103
104
	<!-- make sure we can use @Transactional with the Spring aspect -->
105
	<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"/>
106
107
	<!-- import external services -->
108
    <bean id="thingyRepository" class="com.zfabrik.springframework.ComponentFactoryBean">
109
        <property name="componentName"  value="com.zfabrik.samples.springds-hibernate.domain/repository" />
110
        <property name="className"  value="com.zfabrik.samples.spring_hibernate.thingies.ThingyRepository" />
111
    </bean>
112
</code></pre>