Wednesday, April 4, 2012

Creating an Enterprise Application Repository in Eclipse

In this message, I will create an Enterprise Application Repository Project with a WS, a JPA and an EJB project. I assume that you already know how to create a WS project. Here you can find how to create a JPA project (including a special persistence.xml file to deploy in JBoss 7).

Therefore, we start by the EJB project.


We need to add an EJB:


picking the following options:



and we insert the following code. Note the @Stateless and the @PersistentContext annotations. The latter will allow you to insert objects into the database, using the em.persist() method. The same for the queries:
package ejb;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import pacientes.Paciente;

import medicos.Medico;
import moradas.Morada;

@Stateless
public class EJBimplementation {
@PersistenceContext(name="Proj2-JPA")
EntityManager em;
public Medico[] listarMedicos() {
Query query = em.createQuery("from Medico m");
@SuppressWarnings("unchecked")
List<Medico> lstmedicos = query.getResultList();
return lstmedicos.toArray(new Medico[0]);
}
public void iniciarmedicos() {
Date d [] = { new GregorianCalendar(1980, 12, 23).getTime(), 
new GregorianCalendar(1960, 4, 12).getTime(), 
new GregorianCalendar(1970, 11, 3).getTime() };  
Medico [] medicos = { new Medico(d[0], "A Nibal"), 
new Medico(d[1], "Ra Miro"), 
new Medico(d[2], "Al Fredo") }; 
Morada [] moradas = { new Morada("R. Marques de Pombal, 428""3000-233""Coimbra"),
new Morada("R. Marques de Pombal, 430""3000-233""Coimbra"),
new Morada("R. Marques de Pombal, 432""3000-233""Coimbra")
};

for (int i = 0; i < medicos.length; i++) {
Medico m = medicos[i];
m.setMorada(moradas[i]);
em.persist(moradas[i]);
em.persist(m);
}
}

public Paciente[] listarPacientes() {
Query query = em.createQuery("from Paciente p");
@SuppressWarnings("unchecked")
List<Paciente> lstmedicos = query.getResultList();
return lstmedicos.toArray(new Paciente[0]);
}
public void iniciapacientes() {
Date d [] = { new GregorianCalendar(1980, 12, 23).getTime(), 
new GregorianCalendar(1960, 4, 12).getTime(), 
new GregorianCalendar(1970, 11, 3).getTime() };  
Paciente [] pacientes = { new Paciente(d[0], "A Nibal"), 
new Paciente(d[1], "Ra Miro"), 
new Paciente(d[2], "Al Fredo") }; 
Morada [] moradas = { new Morada("R. Marques de Marialva, 228""3000-233""Cantanhede"),
new Morada("R. Marques de Marialva, 230""3000-233""Cantanhede"),
new Morada("R. Marques de Marialva, 232""3000-233""Cantanhede")
};

for (int i = 0; i < pacientes.length; i++) {
Paciente m = pacientes[i];
m.setMorada(moradas[i]);
em.persist(moradas[i]);
em.persist(m);
}
}

}


Now, let's create the EAR project:


We assume that its name is DemoEAR and that we already have the three other projects (2nd figure):





In the web service project (Proj2-WS according to the example), we need to have something like this to refer to the EJB and to use the methods we defined in the EJB (note the correspondence):


package ejb

import javax.ejb.EJB;
import javax.jws.WebMethod;
import javax.jws.WebService;

import pacientes.Paciente;

import medicos.Medico;
import ejb.EJBimplementation;

@WebService
public class Proj2WS {
@EJB
private EJBimplementation ejb;

public Proj2WS() {
}

@WebMethod
public Medico[] listarMedicos() {
return ejb.listarMedicos();
}

@WebMethod
public void iniciarmedicos() {
ejb.iniciarmedicos();
}

@WebMethod
public Paciente[] listarPacientes() {
return ejb.listarPacientes();
}

@WebMethod
public void iniciarpacientes() {
ejb.iniciapacientes();
}
}


In this way, we refer to the JPA project in the EJB project, and from the WS project we use the EJB we built before. Assuming that all dependencies are satisfied we need the deploy the project as follows:


The entire work will be available as a web service. Just go to the JBoss management console.


No comments:

Post a Comment