viernes, 18 de agosto de 2017

JEE & JSF 7th Part: Using JPA 2.1 (1/2). JPA overview

0. Introduction

To show formatted text Hilite is used

Our goal is to integrate JPA 2.1 in our JSF application.

To achieve this objective these steps will proceed:

  1. Create a Postgres database (my_jpa_test_db) and a new schema (my_jpa_test_sch) in this database 
  2. Adding the Postgres and Hibernate dependencies to pom.xml
  3. Define persistence units in the persistence.xml (with Hibernate JPA implementation)
  4. Define an entity class and Test JPA
  5. Integrating JPA and JSF

1. Create a Postgres database and schema

To create a Postgres database, we need to:

  1. Install PostgreSql. Depending on your operating system, you should google to find how to install.
  2. Install PgAdmin. The process to install is similar to the previous step.
  3. Run PgAdmin and right-click your database server and create a database (for instance my_jpa_test_db)
  4. In your database, you can see Schemas folder. Right-click and create a new schema (for instance my_jpa_test_sch)
  5. Create a user with his password (in this post user="user" ana password="password")

2. Requiered dependencies for maven in the pom.xml file


JPA and Hibernate dependencies are requiered, so we need to add this piece of code to the properties section of the pom.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<!--  JPA Hibernate -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.10.Final</version>
    </dependency>
    
    <!--  PostgreSQL -->
    <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>9.4.1212</version>
    </dependency>
  </dependencies> 

The final pom.xml file is


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.ximodante.jsf</groupId>
  <artifactId>JSFv01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>JSFv01</name>
  <description>JSF 2.2 &amp; CDI</description>
 
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <failOnMissingWebXml>false</failOnMissingWebXml>
  </properties>
  
  <dependencies>
     
    <!-- Servlet 3.1 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    
    <!-- To solve Tomcat problem : java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    
    
    <!--  JSF 2.2 API -->
    <dependency>
     <groupId>com.sun.faces</groupId>
     <artifactId>jsf-api</artifactId>
     <version>2.2.14</version>
    </dependency>

    <!--  JSF 2.2 Implementation -->
    <dependency>
     <groupId>com.sun.faces</groupId>
     <artifactId>jsf-impl</artifactId>
     <version>2.2.14</version>
    </dependency>

    <!--  Primefaces -->
    <dependency>
     <groupId>org.primefaces</groupId>
     <artifactId>primefaces</artifactId>
     <version>6.1</version>
    </dependency>

    <!--  Primefaces Themes -->
    <dependency>
     <groupId>org.primefaces.extensions</groupId>
     <artifactId>all-themes</artifactId>
     <version>1.0.8</version>
     <type>pom</type>
    </dependency>
    
    <!-- Weld CDI for Tomcat (does not fulfill all capabilities !!!) -->
    <dependency>
      <groupId>org.jboss.weld.servlet</groupId>
      <artifactId>weld-servlet-shaded</artifactId>
      <version>3.0.0.Final</version>
    </dependency>
    
    <!-- Validation API Optional -->
    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>2.0.0.CR3</version>
    </dependency>
        
    <!-- Hibernate Bean Validator Optional -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>5.4.1.Final</version>
    </dependency>
    
    <!--  Lombok for setters and getters -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.18</version>
    </dependency>
    
    <!-- JSON -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.0</version>
    </dependency>

    <!-- Apache Commons Utils -->
    <dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-lang3</artifactId>
     <version>3.6</version>
    </dependency>
    
    <!--  JPA Hibernate -->
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.2.10.Final</version>
    </dependency>
    
    <!--  PostgreSQL -->
    <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>9.4.1212</version>
    </dependency>
  </dependencies> 
  
  
</project>

3. The persistence.xml file

The persistence.xml file will be created in the src/main/resources/META-INF folder.

Let's create a persistence unit named "my_jpa_test" that points to the database "my_jpa_test_db" from the local Postgresql database server.

Be careful to put your correct user and password to connect the database.

There are some aspects like connection pool that will not be explained.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="UTF-8"?>
<!-- Persistence deployment descriptor for dev profile -->
<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="my_jpa_test" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      
      <properties>
      
        <!-- Configuring JDBC properties -->
        <property name="javax.persistence.target-database" value="PostgreSQL" />
        <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/my_jpa_test_db" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="password" />
          
        <!-- Hibernate properties -->
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
        

        <!-- Configuring Connection Pool -->
        <property name="hibernate.c3p0.min_size" value="5" />
        <property name="hibernate.c3p0.max_size" value="20" />
        <property name="hibernate.c3p0.timeout" value="500" />
        <property name="hibernate.c3p0.max_statements" value="50" />
        <property name="hibernate.c3p0.idle_test_period" value="2000" />
      
            
      </properties>
   </persistence-unit>
   
    
</persistence>


4. Entity class and JPA tests

Let's define a simple class "Person" with an id and a name for persisting and another one for a quick test called "TestEntityManager".

Person.java:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.ximodante.utils.jpa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

// JPA
@Entity
@Table(name = "person", schema="my_jpa_test_sch") 

// Lombok
@NoArgsConstructor 
public class Person implements Serializable{

 @Transient
 private static final long serialVersionUID = 1L;
    
 // JPA
 @Id
 @Column(name = "id")
 @GeneratedValue(strategy = GenerationType.AUTO)
 // Lombok
 @Getter @Setter
 private Long id;
  
 // JPA
 @Column(name = "name")
 // Lombok
 @Getter @Setter
 private String name;
 
 // Constructor
 public Person(String name){
  this.name=name;
 }
 
}


Note the annotations for both JPA and Lombok (to avoid getters and setters and the no arguments constructor)

The TestEntityManager.java:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package org.ximodante.utils.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class TestEntityManager {
 /**
   * @param args
   */
  EntityManagerFactory emf = null;
  EntityManager em = null;
   
  public static void main(String[] args) {
   // TODO Auto-generated method stub
  
   EntityManagerFactory emf = Persistence.createEntityManagerFactory("my_jpa_test");
   EntityManager em = emf.createEntityManager();
    
   persistSomeData(em);
   em.close();
   emf.close();
  }
   
  private static void persistSomeData (EntityManager em ) {
   em.getTransaction().begin();
     
     em.persist (new Person("Joel")); 
     em.persist (new Person("Xavi"));  
     em.persist (new Person("Ximo"));
     em.persist (new Person("Maria"));
     em.persist (new Person("Henry"));
     
      em.getTransaction().commit();   
  }
  
 }

If we right-click this class (TestEntityManager) and run as Java application. We can see that the table person has been created in the schema my_jpa_test_sch from the database my_jpa_test_db and additionally 5 persons (Joel, Xavi Ximo, Maria, and Henry) have been inserted into this table.

No hay comentarios:

Publicar un comentario

JEE & JSF16th Part: Creating an abstraction view layer to JSF components and Forms (5/5). Frequent problems

1. ERROR #1: Using a bean that does not exists In the previos entry we used this facelet file: 1 2 3 4 5 6 7 8 9 10 11 1...