viernes, 29 de julio de 2011

CREACIÓN Y DESPLIEGUE DE UN PROYECTO EAR, USANDO ECLIPSELINK-JPA

1. Descargar:
Glassfish3.1 o gf3.0.1
Driver de la bd que vayas a usar- postgresql.jar
EclipseLink 2.3.0 Installer Zip (28 MB)
enlace: http://www.eclipse.org/eclipselink/downloads/

2.Crear un proyecto EJB en eclipse

3. Crear en la carpeta META-INF, el archivo persistence.xml.

Persistence.xml

< persistence version="2.0" 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_2_0.xsd" >
< persistence-unit name="EmployeeService" transaction-type="JTA" >
< jta-data-source >jdbc/example
< class >consultorio.Employee
< properties >
< property name="eclipselink.logging.level" value="FINEST" />
< property name="eclipselink.ddl-generation" value="drop-and-create-tables" />

jdbc/example -> recurso bd sql.datasource en glassfish

4. Crear las entidades con notación @entity, el sessionBean(configurar el persistenceUnit, y el entity Manager), y las interfaces(local y remoto) dentro de un paquete nuevo.

1. Clase Entity JPA
@Entity
//@Table(name="EMP", schema="HR")
@Table(name="EMP")
public class Employee implements Serializable {
@Id
@Column(name = "EMP_ID")
private int id;
@Column(name = "COMM")
private String name;
@Column(name = "SAL")
private long salary;

2. SessionBean

@Stateful (mappedName = "ejb/EmployeeService")
public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
@PersistenceContext(unitName="EmployeeService", type=PersistenceContextType.TRANSACTION)
EntityManager em;
public Employee createEmployee(int id, String name, long salary, byte[] pic) {
Employee emp = new Employee(id);
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
emp = findEmployee(id);
System.out.println(emp);
em.flush();
return emp;
}

3. Interfaces Remotas y Locales
@Local
public interface EmployeeServiceLocal {
public void doAction();

public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
public void removeEmployee(int id);

@Remote
public interface EmployeeServiceRemote{
public void doAction();
public Employee createEmployee(int id, String name, long salary, byte[] pic) ;
public void removeEmp


5. Exportar el EJB creado en un .ear (Enterprise Archive).

6.Copiar la lib del driver de bd en glassfish/domain/domain1/lib.

7. Copiar las librerias de eclipselink(descomprimir el jar descargado) --> en /glassfish/lib :

- eclipselink.jar q se encuentra en la carpeta /jlib
- y los 3 .jar q se encuentran dentro de la carpeta: jlib/jpa

8. Crear el recurso jdni a BD en glassfish jdbc/example y hacer ping para comprobar conexión.

9. Desplegar en glassfish el .ear obtenido, visualizar las tablas creadas en bd.

10. Hacer el cliente del .ear.(app de escritorio, consola en este ej) este debe contener en el builtpath las siguientes .jar: Por favor leer este link antes de agregar el jar del EJB....LIB CONFIGURACION

- jar del Ejb. el cual deberia estar ubicado en /glassfish/domain/domain1/lib

En el cliente se debe realizar la conexión al .ear desplegado en glassfish mediante el jdni, IIOP, o en el caso de Ejb3:

El cliente deberia ser algo como:

Context ctx;
try {
ctx = new InitialContext();
//debes de mapear la clase de tu EJB de esta forma @Stateless(mappedName ="ejb/EmployeeService ")
service = (EmployeeServiceRemote) ctx.lookup("ejb/EmployeeService");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//usas la interface o instanciar el bean(nombre del persistenceUnit mapped en el sessionbean creado).
service.createEmployee(45, "AAA", 45000, "asdf".getBytes());

Observar la conexión y las filas creadas en bd :D has realizado una applicación JEE6! Congrats

Referencias:


The following are the valid values for the org.eclipse.persistence.config.PersistenceUnitProperties:

  • NONE – see none.
  • CREATE_ONLY – see create-tables.
  • DROP_AND_CREATE – see drop-and-create-tables.

If you are using persistence in a Java SE environment and would like to create the DDL files without creating tables, additionally define a Java system property INTERACT_WITH_DB and set its value to false.


Example: persistence.xml file


©