Java EE 7 enterprise application development tutorial using Eclipse Kepler and GlassFish 4

In this tutorial, I am going to use the latest technologies (to date)

NOTE: The second blog of this is available in the JSF 2 Ajax.

I am going to use archetypes to create EAR, EJB and WAR components. First, add the maven repository archetype catalogue http://repo1.maven.org/maven2/archetype-catalog.xml to the eclipse archetype selecting the Windows->Preferences and then Maven.

image

Fig: 1 add maven archetype catalogue.

The next step is crating a web project. As shown in the following fig 2, you can find webapp-javaee7. If you have little knowledge about maven, you can create new web application.

image

Fig 2: select archetype to create WAR, EAR and EJB

For example as shown in the following fig 3, you need to specify the Group Id, Artifact Id and Package. For both WAR and EJB project you will get the following error but it can be ignored.

“Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-dependency-plugin:2.6:copy (execution: default, phase: validate”

This error is due to the problem in the m2e plugin.

image

Fig 3: create Artifacts

The web component you have to configure as shown in the following facets. You need to configure JSF library for shake of development which is not required in the runtime. All the ticked options with the relevant versions which will create custom configuration.

image

Fig 4: Web Facets configuration

Here the EJB application configuration

image

Fig 5: EJB facets configuration

Here the configuration for the ERA

image

Fig 6": ERA Facets configuration

For simplicity, the Entity Department has been created in the eafistejb (EJB component)

package au.com.blogspot.ojitha.eafirstejb.domain;  
  
import java.io.Serializable;  
import java.lang.Long;  
import java.lang.String;  
import javax.persistence.\*;  
  
/\*\*  
 \* Entity implementation class for Entity: Department  
 \*  
 \*/  
@Entity  
  
public class Department implements Serializable {  
  
         
    @Id  
    @GeneratedValue(strategy=GenerationType.AUTO)  
    private Long deptId;  
    private String name;  
    private static final long serialVersionUID = 1L;  
  
    public Department() {  
        super();  
    }     
    public Long getDeptId() {  
        return this.deptId;  
    }  
  
    public void setDeptId(Long deptId) {  
        this.deptId = deptId;  
    }     
    public String getName() {  
        return this.name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
     
}  

To manipulate this entity, DepartmentService has been created. This service is implemented in the stateless session bean as shown in the following table

package au.com.blogspot.ojitha.eafirstejb;  
  
import javax.ejb.LocalBean;  
import javax.ejb.Stateless;  
import javax.persistence.EntityManager;  
import javax.persistence.PersistenceContext;  
import javax.persistence.PersistenceUnit;  
  
import au.com.blogspot.ojitha.eafirstejb.domain.Department;  
  
/\*\*  
 \* Session Bean implementation class DepartmentService  
 \*/  
@Stateless(name="deptService")  
@LocalBean  
public class DepartmentService {  
  
    @PersistenceContext  
    private EntityManager em;  
    /\*\*  
     \* Default constructor.   
     \*/  
    public DepartmentService() {  
  
    }  
      
    public void save(String name){  
        Department dept = new Department();  
        dept.setName(name);  
        em.persist(dept);  
    }  
  
}  

There is only single method “save” to save the entity. In addition to that, ejb is named as “deptService”.

This entity saved in to the “hr” database which has been created using MySQL. Glassfish 4 has a problem of creating connection pool. To resolve this problem you have to change the “Url” proper under the “Additional properties”

  • Change the Url to URL
  • specify the complete url as “jdbc:mysql://127.0.0.1:3306/hr?user=ojitha&password=pass”

Using this connection pool, in the GalssFish 4, created the JDBC Resource with the JNDI Name “jdbc/hr”. This has been used in the persistence.xml file as shown in the following listing\

<?xml version="1.0" encoding="UTF-8"?>  
<persistence version="2.1"  
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence\_2\_1.xsd">  
    <persistence-unit name="eafirstejb" transaction-type="JTA">  
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>  
        <jta-data-source>jdbc/hr</jta-data-source>  
        <exclude-unlisted-classes>false</exclude-unlisted-classes>  
        <properties>  
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />  
        </properties>  
    </persistence-unit>  
</persistence>

In the above listing, the persistence unit is eafirstejb and the most important is transaction type which is JTA. Each time when you shutdown the server, department table in the Hr, will be deleted because of the eclipse ddl generation.

In the eafirstweb which is the web application has the following managed bean

package au.com.blogspot.ojitha.eafirstweb.managedbeans;  
  
import javax.ejb.EJB;  
import javax.faces.bean.ManagedBean;  
import javax.faces.bean.SessionScoped;  
  
import au.com.blogspot.ojitha.eafirstejb.DepartmentService;  
  
@ManagedBean(name="dept")  
@SessionScoped  
public class DepartmentManagedBean {  
    @EJB(beanName="deptService")  
    DepartmentService deptService;  
      
    private String name;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
      
    public String addDept(){  
        this.deptService.save(this.name);  
        return "/response.xhtml";  
    }  
  
}  

As shown in the above listing, deptService is the ejb which is already named as “deptService” in the Stateless session bean. For simplicity, the managed bean has been named as “dept” to use in the following facelet. This facelet use the name property to set the name of the department and the addDet to create Department entity via the DepartmentService session bean.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<html xmlns="http://www.w3.org/1999/xhtml"  
    xmlns:ui="http://java.sun.com/jsf/facelets"  
    xmlns:h="http://java.sun.com/jsf/html"  
    xmlns:f="http://java.sun.com/jsf/core">  
    <h:head>  
        <title>department</title>  
    </h:head>  
    <body>  
        <h:form>  
            <p>  
                Department:   
                <h:inputText value="#{dept.name}"/>  
                <h:commandButton value="submit" action="#{dept.addDept()}"/>  
            </p>  
        </h:form>  
    </body>  
  
</html>

when you add the new department, it will be saved into the department table in the MySQL database as shown in the following figure.

image

image

The source code of this blog is available in the GitHub.

Written with StackEdit.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python