Skip to content

20. Case Study – Version 2

We now present the final version of our application:

The new NetBeans project [pam-02] is created by cloning the [pam-01] project:

  • In [1], copy the new project
  • in [2], name the new project [pam-02] and specify its directory
  • in [3], the new project [pam-02]

To "connect" the real [business] layer to the web layer we built, we need to do three things:

  1. delete the simulated [business] layer we created
  2. configure Spring to instantiate the real [business] layer located in the [pam-spring-metier-dao-jpa-hibernate.jar] archive
  3. add all the necessary archives to the project (Spring, Hibernate, JPA, MySQL JDBC driver).

The Spring configuration file [WEB-INF/applicationContext.xml] becomes the following:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  
  <!-- application layers -->
 
 <!-- web -->
  <bean id="config" class="web.Config" init-method="init">
    <property name="business" ref="business"/>
  </bean>
  <!-- business -->
  <bean id="business" class="business.Business">
    <property name="employeeDao" ref="employeeDao"/>
    <property name="contributionDao" ref="contributionDao"/>  
  </bean>
  <!-- dao -->
  <bean id="employeeDao" class="dao.EmployeeDao" />
  <bean id="indemnityDao" class="dao.IndemnityDao" />
  <bean id="contributionDao" class="dao.ContributionDao" />
  
  <!-- JPA configuration -->
  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
      </bean>
    </property>
    <property name="loadTimeWeaver">
      <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
  </bean>
  
  <!-- the DBCP data source -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/dbpam_hibernate" />
    <property name="username" value="root" />
    <property name="password" value="" />
  </bean>
  
  <!-- the transaction manager -->
  <tx:annotation-driven transaction-manager="txManager" />
  <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
  </bean>
  
  <!-- exception translation -->
  <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
  
  <!-- persistence -->
  <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
  
</beans>

Lines 36–41 configure database access.

New libraries (Spring, Hibernate, JPA, MySQL JDBC driver) are added from the [lib] folder [2]. The entire folder is included. There are several dozen .jar files to add [1]:

It was quite difficult to assemble this library because these frameworks sometimes use the same archives. Duplicates must therefore be removed. These files have been gathered in the [lib] folder [2] in the example archive for this document so that the reader does not have to rebuild this library themselves.

That’s it. The new application [pam-02] will now work with the DBMS. Here is a screenshot of a salary calculation:

Image

This time, the calculated salary is the actual salary, not the fictitious salary from version 1. The reader is invited to test the new application.