Tuesday, December 30, 2014

Dispatcher servlet and first Controller

It has come to use MVC pattern, so I create first controller. It is very simple HomeController and it is manage the view component, in this cas the home.jsp page.

To take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource I must create the dispatcher-servlet.xml file.

The way how dispatcher-servlet works looks at the picture below:

So, the The dispatcher servlet is the bit that "knows" to call that method when a browser requests the page, and to combine its results with the matching JSP file to make an html document.

Without create dispatcher-servlet.ml I must configure in the application the web.xml file. In the web.xml file there are defines everything about the application that a server needs to know. It is placed in the /WEB-INF/ directory of the application. The <servlet> element declares the DispatcherServlet. When the DispatcherServlet is initialized, the framework try to load the application context from a file named applicationContext.xml located in /WEB-INF/conf directory. So, I've created the dispatcher-servlet.xml and the <servlet-mapping> lement in web.xml file, which  specifies what URLs will be handled by the DispatcherServlet.

for now web.xml file looks:
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xmlns="http://java.sun.com/xml/ns/javaee"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
      id="WebApp_ID" version="3.0">  
      <display-name>PizzaDeliverySystem</display-name>  
      <welcome-file-list>  
           <welcome-file>home.htm</welcome-file>  
      </welcome-file-list>  
      <context-param>  
           <param-name>contextConfigLocation</param-name>  
 <!--           <param-value> /WEB-INF/conf/applicationContext.xml</param-value> -->  
           <param-value>/WEB-INF/security-context.xml</param-value>  
      </context-param>  
      <context-param>  
           <param-name>log4jConfigLocation</param-name>  
           <param-value>/WEB-INF/conf/log4j.xml</param-value>  
      </context-param>  
      <!-- <context-param> -->  
      <!-- <param-name>spring.profiles.active</param-name> -->  
      <!-- <param-value>dev</param-value> -->  
      <!-- </context-param> -->  
      <!-- <context-param> -->  
      <!-- <param-name>spring.profiles.default</param-name> -->  
      <!-- <param-value>dev</param-value> -->  
      <!-- </context-param> -->  
      <context-param>  
           <param-name>spring.liveBeansView.mbeanDomain</param-name>  
           <param-value>dev</param-value>  
      </context-param>  
     
      <listener>  
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
      </listener>  
      <!-- <listener> -->  
      <!-- org.springframework.web.context.request.RequestContextListener -->  
      <!-- </listener> -->  
      <!-- Creates the Spring Container shared by all Servlets and Filters -->  
      <!-- The definition of the Root Spring Container shared by all Servlets   
           and Filters -->  
      <servlet>  
           <servlet-name>dispatcher</servlet-name>  
           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
           <load-on-startup>1</load-on-startup>  
           <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>WEB-INF/dispatcher-servlet.xml</param-value>  
           </init-param>  
      </servlet>  
      <servlet-mapping>  
           <servlet-name>dispatcher</servlet-name>  
           <url-pattern>/</url-pattern>  
      </servlet-mapping>  
      
 </web-app>  

The dispatcher-servlet.xml file is placed in /WEB-INF/ directory. This is the file where all beans created, such as Controllers, will be placed and defined. So, the HomeController, that is the controller in application is defined here, and will be shown in next steps. The <context:component-scan> tag is used so that the container will know where to search for the classes.

My dispatcher-servlet.xml file looks:

<?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
      xmlns:mvc="http://www.springframework.org/schema/mvc" 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" xmlns:c="http://www.springframework.org/schema/c"  
      xmlns:security="http://www.springframework.org/schema/security"  
      xmlns:task="http://www.springframework.org/schema/task"  
      xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd  
           http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
           http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">  
     
 <context:annotation-config />  
      <!-- Scan for annotations... -->  
      <context:component-scan base-package="com.pizza.delivery"></context:component-scan>  
      <!-- Configures the @Controller programming model -->  
      <mvc:annotation-driven />  
      <jpa:repositories base-package="com.pizza.delivery" />  
      
      </bean>  
      <bean id="viewResolver"  
      class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
      <property name="prefix">  
           <value>/WEB-INF/views/</value>  
      </property>  
      <property name="suffix">  
           <value>.jsp</value>  
      </property>  
 </bean>  
      
 </beans>  

The InternalResourceViewResolver maps the jsp and html files in the ?WEB-INF/ folder and it allows to set properties such as perefix or suffixto the view name to generate the final view page URL.

Finally create HomeContoller and home.jsp page. My first controller is very easy and display "Hello world" on the page and looks:

package com.pizza.delivery.controllers;  
 //import javax.servlet.http.HttpServletRequest;  
 //import javax.servlet.http.HttpServletResponse;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.bind.annotation.RequestMethod;  
 import org.springframework.web.bind.annotation.RequestParam;  
 import org.springframework.web.servlet.ModelAndView;  
 @Controller  
 public class HomeController {  
      @RequestMapping(value="/home", method = RequestMethod.GET)  
      public String home(ModelAndView model) {  
           String message = "Hello World, Spring 3.0!";  
           model.addObject("message", message);  
           return "home";  
      }  
 } 

Annotation @RequestMapping is one of the most widely usedby Spring MVC annotation and is using  for mapping web requests onto specific handler classes and/or handler methods. In above method /home is the URI for which this controller will be used.This concept is very similar to servlet context of a web application.




aaaa

Tuesday, December 23, 2014

Tests

Every good application shuld be create with function test which are checking whether the functionality works as intended. A good practice is to share JUnit tests so that they tested only the selected method or function, starting from the simplest to the more advanced.

In my project I must added dependency for JUnit in pom.xml file, also create new sourceFolder, add package for tests. To import junit classes in Project properties click on "Java Build Path"., next must to do:
  1. Select the Libraries tab.
  2. Click the "Add Library" button.
  3. Choose junit.
Thats it now I may start create test which are check functionality of DAO files. On the first I created test to check adding data to database. In that purpose I create PersonDAOTests file, which looks:
package com.pizza.delivery.tests;  
 import org.junit.After;  
 import org.junit.Assert;  
 import org.junit.Before;  
 import org.junit.Test;  
 import org.junit.runner.RunWith;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.test.context.ContextConfiguration;  
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
 import com.pizza.delivery.domain.Person;  
 import com.pizza.delivery.services.PersonDAO;  
 @RunWith(SpringJUnit4ClassRunner.class)  
 @ContextConfiguration(locations = { "testApplicationContext.xml" })  
 public class PersonDAOTests {  
      @SuppressWarnings("rawtypes")  
      @Autowired  
      private PersonDAO<Person> personDAO;  
      private Person person;  
      @Before  
      public void setUp() {  
           person = new Person();  
           person.setCity("Rzeszow");  
           person.setFirstName("Iwa");  
           person.setLastName("Mat");  
           person.setStatus("");  
           person = personDAO.create(person);  
      }  
      @After  
      public void afterTest() {  
           personDAO.delete(person.getPersonId());  
      }  
      @Test  
      public void testFindByIdPerson() {  
           Person personToCompare = personDAO.findById(person.getPersonId());  
           Assert.assertEquals(personToCompare.getCity(), person.getCity());  
     Assert.assertEquals(personToCompare.getFirstName(), person.getFirstName());  
     Assert.assertEquals(personToCompare.getLastName(), person.getLastName());  
     return;  
           //Person findedPerson = personDAO.findById(person.getPersonId());  
           //Assert.assertNotNull("Find person should be not null", findedPerson);  
           }  
 }  

That test file create object  in database, in Person table. Next using method: 
 public void testFindByIdPerson() compare data from database and from method which adding data to table. At the end of test using method: public void afterTest() to delete object in database.

Similar methods was create in IngredientDAOTest file, but here was usind method:  
public void findByNameTest(),  to searching object for name not for Id. Source code looks:

 @RunWith(SpringJUnit4ClassRunner.class)  
 @ContextConfiguration(locations = { "testApplicationContext.xml" })  
 public class IngredientDAOTests {  
      @Autowired  
      private IngredientDAO<Ingredient> ingredientDAO;  
      private Ingredient ingredient;  
      private static final String IngredientTestName = "PepperTestXXXX";  
      @Before  
      public void setUp() {  
           ingredient = new Ingredient();  
           ingredient.setName(IngredientTestName);  
           ingredient.setPrice(2.0);  
           ingredient = ingredientDAO.create(ingredient);  
      }  
      @After  
      public void afterTests() {  
           ingredientDAO.delete(ingredient.getIngredientId());  
      }  
      @Test  
      public void findByNameTest() {  
           List<Ingredient> listFindByName = ingredientDAO  
                     .findByName(IngredientTestName);  
           assertNotNull(listFindByName);  
           assertEquals(listFindByName.size(), 1);  
           Ingredient ingredientToCompare = listFindByName.get(0);  
           assertEquals(ingredientToCompare.getName(), ingredient.getName());  
           assertEquals(ingredientToCompare.getPrice(), ingredient.getPrice());  
      }  
 }  





Friday, December 19, 2014

DAO layer



In MVC (Model-View-Controller) pattern very often is using DAO (Data Acces Object) component, I may say that is a widely accepted mechanism to abstract away the details of persistence in an application. Data Acces Object supplies uniform interface to comunicate beteween application and data source (i.e. database or file). Owing to DAO layer, application can made some specific operation, typically for acces to data.

Here is how it looks:

Mostly are using CRUD (create-read-update-delete) operation. Thanks to those operation we don't need create and implement many classes and SQL query to manage connection between application and database and input data to them.

In my project DAO layer was divide for two pats:
1. DAO files with AbstractDAO files, which are interfaces;
2. DAO Implement files , hose are classes which imlements above interfaces.

Project tree looks:


In AbstractDAO files defined method, which next are overriding and implements in other DAO files.
AbstractDAO are below:
 package com.pizza.delivery.services;  
 import java.io.Serializable;  
 import java.util.List;  
 public interface AbstractDAO <E, K extends Serializable>{       
      E create(E entity);  
      E update(E entity);  
      void delete(K key);  
      E findById(K key);  
      List<E> listAll();  
 }  
Implemented those operation was in AbstractDAOImpl file which looks:

 package com.pizza.delivery.services.impl;  
 import java.io.Serializable;  
 import java.lang.reflect.ParameterizedType;  
 import java.util.List;  
 import javax.persistence.EntityManager;  
 import javax.persistence.PersistenceContext;  
 import org.hibernate.Criteria;  
 import org.hibernate.Session;  
 import org.springframework.transaction.annotation.Propagation;  
 import org.springframework.transaction.annotation.Transactional;  
 import com.pizza.delivery.services.AbstractDAO;  
 /**  
  * Basic DAO operations dependent with Hibernate's specific classes  
  *   
  * @see EntityManager  
  */  
 @Transactional(propagation = Propagation.REQUIRED, readOnly = false)  
 public abstract class AbstractDAOImpl<E, K extends Serializable> implements  
           AbstractDAO<E, K> {  
      @PersistenceContext  
      protected EntityManager entityManager;  
      protected Class<E> entityClazz;  
      @SuppressWarnings("unchecked")  
      public AbstractDAOImpl() {  
           entityClazz = (Class<E>) ((ParameterizedType) (getClass().getGenericSuperclass()))  
                     .getActualTypeArguments()[0];  
      }  
      public EntityManager getEntityManager() {  
           return entityManager;  
      }  
      public void setEntityManager(EntityManager entity) {  
           this.entityManager = entity;  
      }  
      // /////////////////////////////Create///////////////////////////////////////  
      @Override  
      public E create(E entity) {  
           this.entityManager.persist(entity);  
           this.entityManager.flush();  
           //this.entityManager.merge(entity);  
           return entity;  
      }  
      // /////////////////////////////Update/////////////////////////////////////  
      @Override  
      @Transactional(propagation = Propagation.REQUIRES_NEW)  
      public E update(E entity) {  
           E result = this.entityManager.merge(entity);  
           entityManager.flush();  
           return result;  
      }  
      // ////////////////////////Delete//////////////////////////////////////////  
      @Override  
      @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = IllegalArgumentException.class ,noRollbackFor = IllegalArgumentException.class)  
      public void delete(K key) {  
           E entity = entityManager.find(entityClazz,key);            
                this.entityManager.remove(entity);  
      }  
      // /////////////////////////////FindById///////////////////////////////////  
      @Override  
      public E findById(K key) {  
           return entityManager.find(entityClazz, key);  
      }  
      @SuppressWarnings("unchecked")  
      public List<E> listAll() {  
           final Session session = (Session) entityManager.getDelegate();  
           final Criteria crit = session.createCriteria(entityClazz);  
           return crit.list();  
      }  
 }  

Those method supports most operations to add, update, delate and searching in database. O the project tree except AbstractDAO files there are file which represent all of the domain files, there are entity represent table from database. If we want implements some specific operation typical for one entity we may implement in typical class for the table. For example, in IngredientDAO file defined method:

 package com.pizza.delivery.services;  
 import java.util.List;  
 import com.pizza.delivery.domain.Ingredient;  
 public interface IngredientDAO<Igredient> extends AbstractDAO<Igredient, Long>{  
      List<Ingredient> findByName(String name);  
 }  


Those method will be useful for create new pizza, adding special ingredients in ordering or create my own pizza. Implemented is in in IngredientDAOImpl and looks like below:

package com.pizza.delivery.services.impl;  
 import java.util.List;  
 import javax.persistence.TypedQuery;  
 import javax.persistence.criteria.CriteriaBuilder;  
 import javax.persistence.criteria.CriteriaQuery;  
 import javax.persistence.criteria.ParameterExpression;  
 import javax.persistence.criteria.Root;  
 import org.springframework.stereotype.Repository;  
 import com.pizza.delivery.domain.Ingredient;  
 import com.pizza.delivery.services.IngredientDAO;  
 @Repository  
 public class IngredientDAOImpl extends AbstractDAOImpl<Ingredient, Long>  
           implements IngredientDAO<Ingredient> {  
      @Override  
      public List<Ingredient> findByName(String name) {  
           // TODO Auto-generated method stub  
           CriteriaBuilder cb = entityManager.getCriteriaBuilder();  
           CriteriaQuery<Ingredient> query = cb.createQuery(Ingredient.class);  
           Root<Ingredient> c = query.from(Ingredient.class);  
           ParameterExpression<String> nameParam = cb.parameter(String.class,  
                     "name");  
           query.select(c).where(cb.equal(c.get("name"), nameParam));  
           TypedQuery<Ingredient> typedQuery = entityManager.createQuery(query);  
           typedQuery.setParameter("name", name);  
           return typedQuery.getResultList();  
      }  
 }  


In this class we can see that entityManager using JPA contener getting object from database, I'm using SQL query inject in JPA.










aaaaa

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>  






Monday, December 1, 2014

Learn IT Girl                                                                                    

So, I've decideded join to Learn IT Girl project. What's that? It's international mentorship program, where girls should learn to code.
 I've made first step, now it's time for the rest. Which language should choose, what kind of technology, which development kit, which databases, etc... 

Well, my choice is Java. Would not be too easy, I'll use in project Maven, Spring, hibernate and everything else what will made this project more difficult for beginner programmer.
Lets beginning the game.