Project

General

Profile

Sample-vaadin-spring-hibernate » History » Version 6

Henning Blohm, 07.07.2013 14:07

1 1 Henning Blohm
h1. A sample using Vaadin with Hibernate JPA and Spring on Z2
2
3
This sample is similar to [[Sample-spring-hibernate]] but differs (or rather extends) in that it show cases
4
the use of the "Vaadin":http://www.vaadin.com user interface toolkit in conjunction with Spring implemented annotation based
5
dependency injection over Z2 modularity. 
6
7
As Spring is used throughout - in all modules - this is another practical application of [[How to Spring]].
8
9
This sample is stored in "z2-samples.vaadin-spring-hibernate":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate.
10
11
h2. Prerequisites
12
13
You need to run Java DB as network server on localhost. This is explained next.
14
15
The application will create a database "z2-samples"
16
17
{{include(How to run Java db)}}
18
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.vaadin-spring-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/vaadin-spring-hibernate. You should see this:
40
41
!vaadin-spring-hibernate.png!
42
43
h2. Details
44
45 3 Henning Blohm
As in the other samples we have a re-use domain module. That is a recurring theme for many good reasons. In this case, the domain module "com.zfabrik.samples.vaadin-spring-hibernate.domain":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/show/com.zfabrik.samples.vaadin-spring-hibernate.domain is essentially like the similarly named module of [[Sample-spring-hibernate]]. The only difference is some more data access methods in the "ThingyRepository":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.domain/java/src.api/com/zfabrik/samples/vaadin_spring_hibernate/thingies/ThingyRepository.java.
46 1 Henning Blohm
47 3 Henning Blohm
The Vaadin Web application is defined in the module "com.zfabrik.samples.vaadin-spring-hibernate.web":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/show/com.zfabrik.samples.vaadin-spring-hibernate.web. It has the usual Spring application context in "web/WebContent/WEB-INF/applicationContext.xml":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/web/WebContent/WEB-INF/applicationContext.xml that imports the thingy repository:
48 1 Henning Blohm
49 3 Henning Blohm
<pre><code class="xml">
50
        <!-- import external components -->
51
        <bean id="thingyRepository" class="com.zfabrik.springframework.ComponentFactoryBean">
52
                <property name="componentName" value="com.zfabrik.samples.vaadin-spring-hibernate.domain/repository" />
53
                <property name="className" value="com.zfabrik.samples.vaadin_spring_hibernate.thingies.ThingyRepository" />
54
        </bean>
55
</code></pre>
56
57 5 Henning Blohm
One particularity of using Vaadin in a modular environment is that Vaadin loads it's application class using the class loader that loaded the Vaadin classes (rather than the current thread's context class loader - as would be advised normally). When the Web application module is separated from the Vaadin module this can obviously not work too well (see also "Enhancement #9809":http://dev.vaadin.com/ticket/9809). To fix that you can tell Vaadin to use a custom class loader implementation (that - again - is loaded with the Vaadin class loader). That's why we declare the Vaadin servlet in "web/WebContent/WEB-INF/web.xml":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/web/WebContent/WEB-INF/web.xml list this:
58
59
<pre><code class="xml">
60
<!-- Vaadin -->
61
<servlet>
62
  <servlet-name>VaadinServlet</servlet-name>
63
  <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
64
  <init-param>
65
    <param-name>application</param-name>
66
    <param-value>com.zfabrik.samples.impl.vaadin_spring_hibernate.ApplicationImpl</param-value>
67
  </init-param>
68
  <!-- this because Vaadin doesn't use the thread's context class loader by default -->
69
  *<init-param>
70
    <param-name>ClassLoader</param-name>
71
    <param-value>com.zfabrik.vaadin.ContextClassLoader</param-value>
72
  </init-param>*
73
</servlet>
74
</code></pre>
75
76 3 Henning Blohm
The Vaadin application class "ApplicationImpl":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate/ApplicationImpl.java constructs a simple view hierarchy that containes a table view based on a "lazy query container add-on":https://vaadin.com/directory#addon/lazy-query-container data model that is fed from the domain module. To do so the corresponding query implementation (in lazy query container speak) has the repository injected:
77
78
<pre><code class="java">
79
@Configurable
80
public class ThingiesQuery implements Query {
81
	@Autowired
82
	private ThingyRepository repository;
83
	private Integer size;
84
	private boolean asc;
85
86
// ...
87
}
88
</code></pre>
89 5 Henning Blohm
90
Please browse the actual UI code at "com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/show/com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate. It pretty much speaks for itself.
91 4 Henning Blohm
92
One important note on transaction management: In this example, transaction boundaries are enforced by a servlet filter once more. It is implemented in "TransactionFilter":http://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/entry/com.zfabrik.samples.vaadin-spring-hibernate.web/java/src.impl/com/zfabrik/samples/impl/vaadin_spring_hibernate/util/TransactionFilter.java and essentially looks like this:
93
94
<pre><code class="java">
95
public class TransactionFilter implements Filter {
96
97
98
        @Transactional
99
        @Override
100
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
101
                chain.doFilter(request, response);
102
        }
103
104
}
105
</code></pre>
106
107
In other words, the only thing it really does is to make sure Spring spans a transaction (using the underlying JTA implementation) along the request. It is tempting to implement transaction demarcation in the Vaadin Application class. However, we cannot use the <code>@Transactional</code> annotation as there is no single request spanning method and using the JTA UserTransaction object is unfortunately not sufficient to "remote control" Spring's JTA wrappers. So the easiest work around is to use Spring TX demarcation right away (see also [[how to transaction management]]).
108 6 Henning Blohm
109
Furthermore this sample illustrates the use of the *ExtensionComponentsUtil* ("javadoc":https://redmine.z2-environment.net/projects/z2-addons/repository/z2-addons-vaadin/revisions/master/entry/com.zfabrik.vaadin/java/src.api/com/zfabrik/vaadin/ExtensionComponentsUtil.java) as described in detail at [[Vaadin_Add-on]]. This utility allows to conveniently modularize the user interface implementation by providing and retrieving Vaadin user interface component implementations across modules.
110
111
In the specific case, the module "com.zfabrik.samples.vaadin-spring-hibernate.extension":https://redmine.z2-environment.net/projects/z2-samples/repository/z2-samples-vaadin-spring-hibernate/revisions/master/show/com.zfabrik.samples.vaadin-spring-hibernate.extension provides an extension component *somethingAboutThingies* that is added as the button "About thingies..." to the button toolbar of the main interface.