Skip to content

22. Conclusion

Let’s review what we’ve done in this document. We examined two [DAO] layers using one of the following two architectures:

The [DAO1] layer was implemented with Spring JDBC and the [DAO2] layer with Spring JPA. The [DAO1] and [DAO2] layers implemented the same [IDAO] interface, which allowed us to write a single test [JUnitTestDao] to test both [DAO] layers;

Once this was done, we exposed the [IDAO] interface on the web as follows:

  • In [1], the [IDAO] layer was exposed on the web through a web layer [2] implemented using Spring MVC. It is indeed the [IDAO] interface that is exposed, and we built two versions of the web service depending on whether this interface is implemented using a [DAO-JDBC] or [DAO-JPA-JDBC] architecture;
  • in [B], a remote client uses the URLs exposed by the web service, which provide access to the methods of the [IDAO-server] layer. We ensured that the [DAO-Client] layer [3] implements the [IDAO-server] interface [1]. This allowed us to reuse the same [JUnitTestDao] test that had already been used twice;
  • In [3], the [DAO-Client] layer was implemented using Spring RestTemplate;

With that done, we secured access to the web service:

  • in [5], the client’s HTTP request passes through an authentication layer implemented with Spring Security;

With that done, we evolved the previous architecture into the following:

  • in [3], the client application is itself a web application served by the web server [4]. The client application displays a form [5] in the browser that allows querying the URLs of the secure web service. HTTP access to the secure web service is handled by a [jS] layer implemented in JavaScript. This architecture utilizes what are known as cross-domain requests:
    • the web service presents URLs of the form [http://machine1:port1/];
    • the client web application is downloaded from a URL [http://machine2:port2/]. If [http://machine2:port2/] is not identical to [http://machine1:port1/] (same machine, same port), then the client browser will block HTTP calls from the [DAO-client-js] layer. To resolve this issue, the web service must allow cross-domain requests;

The projects presented have been tested with the following six databases:

  • MySQL 5 Community Edition;
  • SQL Server 2014 Express;
  • PostgreSQL 9.4;
  • Oracle Express 11g Release 2;
  • IBM DB2 Express-C 10.5;
  • Firebird 2.5.4;

For each of these DBMSs, we developed four different [DAO] layers:

  • a layer implemented with Spring JDBC;
  • a layer implemented with Spring JPA and the Hibernate JPA provider;
  • a layer implemented with Spring JPA and the EclipseLink JPA provider;
  • a layer implemented with Spring JPA and the OpenJPA JPA provider;

Thus, a set of twenty-four different configurations was presented. A major effort was made toward code refactoring:

  • most of the code is written only once. It is based on two Maven configuration projects:
    • one configures the JDBC layer;
    • the other configures the JPA layer;

The Maven configuration project for the JDBC layer [1] of a specific DBMS allows:

  • import the JDBC driver archive;
  • to define the access credentials for the database being used and the various SQL statements that the [DAO1] layer will send to the JDBC driver. Although SQL is standardized, we encountered portability issues primarily due to the presence in queries of table/column names that turned out to be prohibited keywords in certain DBMSs (the ROLES table for DB2, the PASSWORD column for Firebird). Furthermore, although a column name is normally case-insensitive (upper/lowercase), we encountered an issue with PostgreSQL regarding the ID column of the primary key in the tables. It required that it be named "id" in lowercase;

The three Maven projects for configuring the JPA layer [2] of a specific DBMS allow:

  • importing the JPA implementation archive;
  • configure the JPA implementation used for the specific connected DBMS. In fact, it is the JPA layer that issues SQL commands to the JDBC layer. To be effective, it must know the DBMS in order to send it SQL commands that it will recognize. These commands may use the proprietary SQL of that DBMS as well as its specific features (data types, sequences, triggers, procedures, automatic generation of primary keys, etc.);

We thus created twenty-four Maven configuration projects (4 configurations × 6 DBMS) upon which all other database operation projects were based. In the diagrams above, since the [DAO1] and [DAO2] layers provide the same interface, the 24 configurations of the two architectures above were tested using a single test class [JUnitTestDao]. Once these architectures were verified, there were no further difficulties:

  • the Maven project for publishing the database on the web is based on these two architectures. There are therefore also 24 possible configurations here;
  • the Maven project for securing access to the web service builds on the previous project and also has 24 possible configurations;
  • finally, the Maven project enabling cross-domain requests to the secure web service builds on the previous project and also has 24 possible configurations;

Although this document does not cover all the capabilities of the Java language or all its areas of application, it can be used as a learning resource for the language. Readers who have mastered the content of this course will have reached an “advanced Java” level in both the use of the language and the Spring framework. They can then continue their Java training with the following books:

  • [Spring MVC and Thymeleaf by Example] [http://tahe.developpez.com/java/springmvc-thymeleaf], which continues the exploration of the Spring ecosystem by introducing its "MVC web programming" branch. It uses a more complex database than the one studied here;
  • [AngularJS / Spring MVC Tutorial] [http://tahe.developpez.com/angularjs-spring4] which presents a client/server web architecture, where the client is implemented using the [AngularJS] framework and the server using [Spring MVC];
  • [Introduction to Java EE] [http://tahe.developpez.com/java/javaee], which moves away from the Spring ecosystem to a web architecture based on JSF (Java Server Faces) and EJB (Enterprise JavaBeans);
  • [Introduction to Android Tablet Programming] [http://tahe.developpez.com/android/exemples-intellij-aa], which describes a client/server architecture where the client is an Android tablet programmed in Java and the server is a web service implemented using Spring MVC;