Monday, December 8, 2014

Project configuration

In this post I've create a project. The development tools which I'm going to use during this project are:

  • Oracle Database 11g Express Edition
  • Eclipse Luna for Java EE
  • Apache Tomcat 8.0.15

The technologies I'm going to use are:

  • Maven for the dependency management,
  • JPA to interact with the database,
  • Spring to define the our application context (besides of wiring everything),

and finally I use JUnit to application tests.


So, at first I create Dynamic Web Project. In empty project from Eclipse Marketplace install Maven, Hibernate Tools and Oracle Database tool. 

Next in pom.xml file adding dependencies for: Oracle, Spring, Hibernate and Junit.

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
   http://maven.apache.org/maven-v4_0_0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>PizzaDeliverySystem</groupId>  
      <artifactId>PizzaDeliverySystem</artifactId>  
      <version>0.0.1-SNAPSHOT</version>  
      <packaging>war</packaging>  
      <properties>  
           <spring.version>4.1.1.RELEASE</spring.version>  
           <hibernate.version>4.3.7.Final</hibernate.version>  
           <jdk.version>1.8</jdk.version>  
      </properties>  
      <dependencies>  
           <!-- Spring -->  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context</artifactId>  
                <version>${spring.version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-tx</artifactId>  
                <version>${spring.version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-core</artifactId>  
                <version>${spring.version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-web</artifactId>  
                <version>${spring.version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-webmvc</artifactId>  
                <version>${spring.version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-orm</artifactId>  
                <version>${spring.version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework.data</groupId>  
                <artifactId>spring-data-jpa</artifactId>  
                <version>1.7.1.RELEASE</version>  
           </dependency>  
           <dependency>  
                <groupId>org.springframework.security</groupId>  
                <artifactId>spring-security-core</artifactId>  
                <version>3.2.5.RELEASE</version>  
           </dependency>  
           <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-test</artifactId>  
       <version>3.2.0.RELEASE</version>  
     </dependency>   
           <!-- Hibernate -->  
           <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-core</artifactId>  
                <version>${hibernate.version}</version>  
           </dependency>  
           <dependency>  
                <groupId>org.hibernate</groupId>  
                <artifactId>hibernate-entitymanager</artifactId>  
                <version>${hibernate.version}</version>  
           </dependency>  
           <dependency>  
                <groupId>commons-dbcp</groupId>  
                <artifactId>commons-dbcp</artifactId>  
                <version>1.2.2</version>  
           </dependency>  
           <!-- junit -->  
           <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>4.4</version>  
           </dependency>  
      </dependencies>  
      <build>  
           <finalName>PizzaDeliverySystem</finalName>  
           <plugins>  
                <plugin>  
                     <groupId>org.apache.maven.plugins</groupId>  
                     <artifactId>maven-compiler-plugin</artifactId>  
                     <version>3.2</version>  
                     <configuration>  
                          <webXml>src\main\webapp\WEB-INF\web.xml</webXml>  
                          <source>${jdk.version}</source>  
                          <target>${jdk.version}</target>  
                     </configuration>  
                </plugin>  
                <plugin>  
                     <groupId>org.apache.maven.plugins</groupId>  
                     <artifactId>maven-war-plugin</artifactId>  
                     <version>2.5</version>  
                </plugin>  
           </plugins>  
      </build>  
 </project>  

In Project tree find WebContent folder. Inside WEB-INF create folder - config and
inside create file applicationcontext.xml. 
I need to configure the application context. Inside create configuration for database and Spring. That means, I should add a dataSource within  context file where is define url , user, password and the database which I'm going to use.


<?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
      xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd     
      http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
      http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.1.xsd   
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd      
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd  
      http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">  
      <context:annotation-config />  
      <jpa:repositories base-package="com.pizza.delivery" />  
      <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
           destroy-method="close">  
           <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
           <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />  
           <property name="username" value="hr" />  
           <property name="password" value="hr" />  
      </bean>  
      
 </beans>   

Next adding to web.xml file definition of the Root Spring Container and creates the Spring Container, used to describe any component that can contain other components inside itself.

 <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->  
 <context-param>  
   <param-name>contextConfigLocation</param-name>  
   <param-value>/WEB-INF/conf/applicationContext.xml</param-value>  
 </context-param>  
 <!-- Creates the Spring Container shared by all Servlets and Filters -->  
 <listener>  
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
 </listener>  

To adding database connection in menu click on Window menu, choose Show View and next click on Data Source Explorer. Inside this view right click on Database Connection, check New. In open window check Oracle Database Connection, click Next. Fill the field, click Test Connection, if connection succed click Finish.



On the project tree right click, choose Properties, find Project Facets, check JPA.
Set JPA Version on: EclipseLink 2.5.x add database conection. Next check avalaible tables from database and Apply.

In aplicaationcontext.xml I define an entityManagerFactory to which we pass the newly created dataSource. This entityManagerFactory object will be controlled by a transactionManager. As we are using JPA we need a org.springframework.orm.jpa.JpaTransactionManager.

     <bean 
           class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"  
           id="entityManagerFactory">  
           <property name="persistenceUnitName" value="spring-jpa" />  
           <property name="dataSource" ref="dataSource" />  
      </bean>  
      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
           <property name="entityManagerFactory" ref="entityManagerFactory" />  
      </bean>  
      <bean id='transactionManager'  
           class='org.springframework.orm.hibernate3.HibernateTransactionManager'>  
           <property name='sessionFactory' ref='sessionFactory' />  
      </bean>  
      <bean  
           class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />  


To use JPA in project I'm going to create a persistence unit in the META-INF/persistence.xml file. Within this file it will be a specified persistence unit. To do this should choosing the transaction type. I need to use the provider that will implement JPA. In this case, I'm going to use Hibernate.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  
 http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"  
      version="1.0">  
      <persistence-unit name="JpaPersistenceUnit"  
           transaction-type="RESOURCE_LOCAL">  
           <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>  
           <class>com.pizza.delivery.domain.Drink</class>  
           <class>com.pizza.delivery.domain.Ingredient</class>  
           <class>com.pizza.delivery.domain.Ordering</class>  
           <class>com.pizza.delivery.domain.Person</class>  
           <class>com.pizza.delivery.domain.Pizza</class>  
           <class>com.pizza.delivery.domain.Sauce</class>  
           <properties>  
                <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />  
           </properties>  
      </persistence-unit>  
      <bean id='jdbcTemplate'   
           class='org.springframework.jdbc.core.simple.SimpleJdbcTemplate'>  
           <constructor-arg type='javax.sql.DataSource' ref='dataSource' />  
      </bean>  
 </persistence>  






No comments:

Post a Comment