He aquí un pequeño proyecto hecho con JPA. El proyecto posee una clase principal (Principal.java) la cual tiene varios métodos que muestran el uso que le podemos dar a la Java Persistence API (listar, buscar, crear y eliminar).
persistence.xml
Archivo xml que nos permite colocar los datos de nuestra DB, archivo SQL, permisos, etc.
Las clases Empleado.java y Departamento.java representan a las tablas contenidas en la BD denominada 'curso'. EmpleadoGenerar.java y DepartamentoGenerar.java son subclases de la superclase JpaGenerar.java, en esta clase creamos los objetos EntityManagerFactory y los EntityManager los cuales nos permiten crear la persistencia se las entidades y su manipulación.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO Departamento(nombre,responsable) VALUES ('Direccion general','Thomas Muller'); | |
INSERT INTO Empleado(nombre,apellidos,cargo,sexo,fecha,departamento_nombre) VALUES ('Mariana','Velez','Programador','M','2011-06-02','Direccion general'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.model; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.OneToMany; | |
import java.util.List; | |
@Entity | |
public class Departamento { | |
@Id | |
private String nombre; | |
private String responsable; | |
@OneToMany(mappedBy = "departamento") | |
private List<Empleado> empleados; | |
public Departamento() { | |
} | |
public Departamento(String nombre, String responsable) { | |
super(); | |
this.nombre = nombre; | |
this.responsable = responsable; | |
} | |
public Departamento(String nombre, String responsable, List<Empleado> empleados) { | |
super(); | |
this.nombre = nombre; | |
this.responsable = responsable; | |
this.empleados = empleados; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public String getResponsable() { | |
return responsable; | |
} | |
public void setResponsable(String responsable) { | |
this.responsable = responsable; | |
} | |
public List<Empleado> getEmpleados() { | |
return empleados; | |
} | |
public void setEmpleados(List<Empleado> empleados) { | |
this.empleados = empleados; | |
} | |
@Override | |
public String toString() { | |
return "Departamento [nombre=" + nombre + ", responsable=" + responsable + ", empleados=" + empleados + "]"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.utils; | |
import java.util.List; | |
import javax.persistence.TypedQuery; | |
import org.codemonkey.model.Departamento; | |
public class DepartamentoGenerar extends JpaGenerar { | |
public List<Departamento> getAllDepartamentos(){ | |
JpaGenerar.setupStatic(); | |
this.setup(); | |
TypedQuery<Departamento> query =em.createQuery("SELECT d FROM Departamento d", Departamento.class); | |
List<Departamento> resultado = query.getResultList(); | |
return resultado; | |
} | |
public void generarDepartamento(Departamento departamento) { | |
System.out.println("Generar Departamento "+departamento.getNombre()); | |
JpaGenerar.setupStatic(); | |
this.setup(); | |
em.getTransaction().begin(); | |
em.persist(departamento); | |
em.getTransaction().commit(); | |
System.out.println("Departamento "+departamento.getNombre()+" creado"); | |
this.tearDown(); | |
JpaGenerar.tearDownStatic(); | |
} | |
public void encontrarDepartamento(String buscar) { | |
JpaGenerar.setupStatic(); | |
this.setup(); | |
Departamento departamentoEncontrado = em.find(Departamento.class, buscar); | |
if(departamentoEncontrado!=null) { | |
System.out.println("Departamento "+departamentoEncontrado.getNombre()+" encontrado !!"); | |
System.out.println("Responsable: "+departamentoEncontrado.getResponsable()); | |
} | |
this.tearDown(); | |
JpaGenerar.tearDownStatic(); | |
} | |
public void borrarDepartamento(String eliminar) { | |
JpaGenerar.setupStatic(); | |
this.setup(); | |
Departamento departamentoEliminar = em.find(Departamento.class, eliminar); | |
String nombre = departamentoEliminar.getNombre(); | |
if(departamentoEliminar!=null) { | |
em.getTransaction().begin(); | |
em.remove(departamentoEliminar); | |
em.getTransaction().commit(); | |
System.out.println("Departamento "+nombre+" eliminado!!"); | |
} | |
this.tearDown(); | |
JpaGenerar.tearDownStatic(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.test; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertNull; | |
import org.codemonkey.model.Departamento; | |
//import org.junit.Before; | |
import org.junit.Test; | |
//import javax.persistence.EntityManager; | |
//import javax.persistence.EntityManagerFactory; | |
//import javax.persistence.Persistence; | |
public class DepartamentoTest extends TestUnitJpa{ | |
@Test | |
public void entityManagerFactoryOK() { | |
assertNotNull(emf); | |
} | |
@Test | |
public void entityManagerOK() { | |
assertNotNull(em); | |
} | |
@Test | |
public void getDepartamento() { | |
Departamento departamento = em.find(Departamento.class,"Direccion general"); | |
assertEquals("Thomas Muller", departamento.getResponsable()); | |
} | |
@Test | |
public void deleteDepartamento() { | |
Departamento departamento= em.find(Departamento.class, "Direccion general"); | |
em.getTransaction().begin(); | |
em.remove(departamento); | |
em.getTransaction().commit(); | |
Departamento departamentoBorrado= em.find(Departamento.class, "Direccion general"); | |
assertNull(departamentoBorrado); | |
} | |
@Test | |
public void insertDepartamento() { | |
em.getTransaction().begin(); | |
Departamento departamentoNuevo= new Departamento("Recursos humanos","Cecilia Mora"); | |
em.persist(departamentoNuevo); | |
em.getTransaction().commit(); | |
Departamento departamentoInsertado= em.find(Departamento.class,"Recursos humanos"); | |
assertNotNull(departamentoInsertado); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.model; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.ManyToOne; | |
import java.util.Date; | |
@Entity | |
public class Empleado { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private int id; | |
private String nombre; | |
private String apellidos; | |
private String cargo; | |
private char sexo; | |
private Date fecha; | |
@ManyToOne | |
@JoinColumn(name = "departamento_nombre") | |
private Departamento departamento; | |
public Empleado() { | |
} | |
public Empleado(int id) { | |
super(); | |
this.id = id; | |
} | |
public Empleado(String nombre, String apellidos, String cargo, char sexo, Date fecha, Departamento departamento) { | |
super(); | |
this.nombre = nombre; | |
this.apellidos = apellidos; | |
this.cargo = cargo; | |
this.sexo = sexo; | |
this.fecha = fecha; | |
this.departamento = departamento; | |
} | |
public Empleado(int id, String nombre, String apellidos, String cargo, char sexo, Date fecha) { | |
super(); | |
this.id = id; | |
this.nombre = nombre; | |
this.apellidos = apellidos; | |
this.cargo = cargo; | |
this.sexo = sexo; | |
this.fecha = fecha; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public String getApellidos() { | |
return apellidos; | |
} | |
public void setApellidos(String apellidos) { | |
this.apellidos = apellidos; | |
} | |
public String getCargo() { | |
return cargo; | |
} | |
public void setCargo(String cargo) { | |
this.cargo = cargo; | |
} | |
public char getSexo() { | |
return sexo; | |
} | |
public void setSexo(char sexo) { | |
this.sexo = sexo; | |
} | |
public Date getFecha() { | |
return fecha; | |
} | |
public void setFecha(Date fecha) { | |
this.fecha = fecha; | |
} | |
public Departamento getDepartamento() { | |
return departamento; | |
} | |
public void setDepartamento(Departamento departamento) { | |
this.departamento = departamento; | |
} | |
@Override | |
public String toString() { | |
return "Empleado [id=" + id + ", nombre=" + nombre + ", apellidos=" + apellidos + ", cargo=" + cargo + ", sexo=" | |
+ sexo + ", fecha=" + fecha + "]"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.utils; | |
import org.codemonkey.model.Empleado; | |
import java.util.List; | |
import javax.persistence.TypedQuery; | |
public class EmpleadoGenerar extends JpaGenerar { | |
public List<Empleado> getAllEmpleados(){ | |
JpaGenerar.setupStatic(); | |
this.setup(); | |
TypedQuery<Empleado> query =em.createQuery("SELECT e FROM Empleado e", Empleado.class); | |
List<Empleado> resultado = query.getResultList(); | |
return resultado; | |
} | |
public void generarEmpleado(Empleado empleado) { | |
System.out.println("Generar Empleado " + empleado.getCargo()); | |
JpaGenerar.setupStatic(); | |
this.setup(); | |
em.getTransaction().begin(); | |
em.persist(empleado); | |
em.getTransaction().commit(); | |
System.out.println("Empleado " + empleado.getCargo() + " creado"); | |
this.tearDown(); | |
JpaGenerar.tearDownStatic(); | |
} | |
public void encontrarEmpleado(int buscar) { | |
JpaGenerar.setupStatic(); | |
this.setup(); | |
Empleado empleadoEncontrado = em.find(Empleado.class, buscar); | |
if(empleadoEncontrado!=null) { | |
System.out.println("Empleado con id "+empleadoEncontrado.getId()+" encontrado!!"); | |
System.out.println("Nombre: "+empleadoEncontrado.getNombre()+" "+empleadoEncontrado.getApellidos()); | |
System.out.println("Cargo: "+empleadoEncontrado.getCargo()); | |
System.out.println("Sexo: "+(empleadoEncontrado.getSexo()=='M'? "Masculino" : "Femenino")); | |
System.out.println("Departamento: "+empleadoEncontrado.getDepartamento().getNombre()); | |
} | |
this.tearDown(); | |
JpaGenerar.tearDownStatic(); | |
} | |
public void borrarEmpleado(int eliminar) { | |
JpaGenerar.setupStatic(); | |
this.setup(); | |
Empleado empleadoEliminar = em.find(Empleado.class, eliminar); | |
String cargo = empleadoEliminar.getCargo(); | |
if(empleadoEliminar!=null) { | |
em.getTransaction().begin(); | |
em.remove(empleadoEliminar); | |
em.getTransaction().commit(); | |
System.out.println("Empleado "+cargo+" eliminado!!"); | |
} | |
this.tearDown(); | |
JpaGenerar.tearDownStatic(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.test; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
import org.codemonkey.model.Departamento; | |
import org.codemonkey.model.Empleado; | |
import org.junit.Test; | |
public class EmpleadoTest extends TestUnitJpa { | |
@Test | |
public void entityManagerFactoryOK() { | |
assertNotNull(emf); | |
} | |
@Test | |
public void entityManagerOK() { | |
assertNotNull(em); | |
} | |
@Test | |
public void selectEmpleado() { | |
Empleado empleado = em.find(Empleado.class,1); | |
assertEquals("Fernando",empleado.getNombre()); | |
} | |
@Test | |
public void seleccionarComentarioInicialConsulta() { | |
int total= em.createQuery("select e from Empleado e").getResultList().size(); | |
assertEquals(1,total); | |
} | |
@Test | |
public void seleccionarComentarioDeNoticia() { | |
Departamento departamento= em.find(Departamento.class, "Direccion general"); | |
Empleado empleado = departamento.getEmpleados().get(0); | |
assertEquals("Fernando",empleado.getNombre()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.utils; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
public abstract class JpaGenerar { | |
public static final String BD = "curso"; | |
public static EntityManagerFactory emf; | |
protected EntityManager em; | |
public static void setupStatic() { | |
Persistence.generateSchema(BD, null); | |
emf = Persistence.createEntityManagerFactory(BD); | |
System.out.println("Creamos la persistencia"); | |
} | |
public void setup() { | |
em = emf.createEntityManager(); | |
System.out.println("Se crea la EntityManager"); | |
} | |
public void tearDown() { | |
em.clear(); | |
em.close(); | |
System.out.println("Se cierra el EntityManager"); | |
} | |
public static void tearDownStatic() { | |
emf.close(); | |
System.out.println("Se cierra el EntityManagerFactory"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<persistence 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" | |
version="2.1"> | |
<persistence-unit name="curso"> <!-- transaction-type="RESOURCE_LOCAL"> --> | |
<properties> | |
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" /> <!-- DB Driver --> | |
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/curso" /> <!-- BD Mane --> | |
<property name="javax.persistence.jdbc.user" value="postgres" /> <!-- DB User --> | |
<property name="javax.persistence.jdbc.password" value="5432" /> <!-- DB Password --> | |
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <!-- DB Dialect --> | |
<property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update --> | |
<property name="hibernate.show_sql" value="true" /> <!-- Show SQL in console --> | |
<property name="hibernate.format_sql" value="true" /> <!-- Show SQL formatted --> | |
<property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/> | |
</properties> | |
</persistence-unit> | |
</persistence> | |
<!-- <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_2_0.xsd" | |
version="2.0"> | |
<persistence-unit name="curso"> | |
<properties> | |
<property name="hibernate.show_sql" value="true" /> | |
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> | |
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:curso;"/> | |
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/> | |
<property name="javax.persistence.schema-generation.database.action" | |
value="drop-and-create" /> | |
<property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/> | |
</properties> | |
</persistence-unit> | |
</persistence> --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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>com.codemonkey</groupId> | |
<artifactId>CursoJpa</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>CursoJpa</name> | |
<description>Curso JPA</description> | |
<dependencies> | |
<dependency> | |
<groupId>org.hibernate.javax.persistence</groupId> | |
<artifactId>hibernate-jpa-2.1-api</artifactId> | |
<version>1.0.0.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-core</artifactId> | |
<version>5.2.12.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.projectlombok</groupId> | |
<artifactId>lombok</artifactId> | |
<version>1.18.4</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
<version>9.1-901-1.jdbc4</version> | |
</dependency> | |
<!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> | |
<version>6.0.6</version> </dependency> --> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>com.h2database</groupId> | |
<artifactId>h2</artifactId> | |
<version>1.4.196</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.app; | |
import org.codemonkey.utils.DepartamentoGenerar; | |
import org.codemonkey.utils.EmpleadoGenerar; | |
import org.codemonkey.utils.JpaGenerar; | |
import org.codemonkey.model.Departamento; | |
import org.codemonkey.model.Empleado; | |
//import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Date; | |
import static java.lang.System.out; | |
import static java.lang.System.err; | |
public class Principal { | |
public static void main(String[] args) { | |
mockDemoA(); | |
mockDemoB(); | |
mockDemoC(); | |
mockDemoD(); | |
mockDemoE(); | |
mockDemoF(); | |
mockDemoG(); | |
mockDemoH(); | |
} | |
public static void mockDemoH() { | |
DepartamentoGenerar departamentoGenerar = new DepartamentoGenerar(); | |
List<Departamento> listaDepartamento = departamentoGenerar.getAllDepartamentos(); | |
out.println("Departamentos:"); | |
for(Departamento departamento: listaDepartamento) { | |
out.println(departamento); | |
} | |
departamentoGenerar.tearDown(); | |
JpaGenerar.tearDownStatic(); | |
System.exit(0); | |
} | |
public static void mockDemoG() { | |
EmpleadoGenerar empleadoGenerar = new EmpleadoGenerar(); | |
List<Empleado> listaEmpleado = empleadoGenerar.getAllEmpleados(); | |
out.println("Empleados:"); | |
for(Empleado empleado: listaEmpleado) { | |
out.println(empleado); | |
} | |
empleadoGenerar.tearDown(); | |
JpaGenerar.tearDownStatic(); | |
System.exit(0); | |
} | |
public static void mockDemoF() { | |
EmpleadoGenerar empleadoEliminar = new EmpleadoGenerar(); | |
int eliminar = 3; | |
try { | |
empleadoEliminar.borrarEmpleado(eliminar); | |
} catch (Exception ex) { | |
err.println("Ha ocurrido una Exception!!"); | |
} | |
System.exit(0); | |
} | |
public static void mockDemoE() { | |
DepartamentoGenerar departamentoEliminar = new DepartamentoGenerar(); | |
String eliminar = "Recursos humanos"; | |
try { | |
departamentoEliminar.borrarDepartamento(eliminar); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
System.exit(0); | |
} | |
public static void mockDemoD() { | |
DepartamentoGenerar departamentoEncontrar = new DepartamentoGenerar(); | |
String buscar = "Ventas"; | |
try { | |
departamentoEncontrar.encontrarDepartamento(buscar); | |
} catch (Exception ex) { | |
err.println("Ha ocurrido una exception!!"); | |
ex.printStackTrace(); | |
} | |
System.exit(0); | |
} | |
public static void mockDemoC() { | |
EmpleadoGenerar empleadoEncontrar = new EmpleadoGenerar(); | |
int buscar = 1; | |
try { | |
empleadoEncontrar.encontrarEmpleado(buscar); | |
} catch (Exception ex) { | |
err.println("Ha ocurrido una exception!!"); | |
ex.printStackTrace(); | |
} | |
System.exit(0); | |
} | |
public static void mockDemoB() { | |
EmpleadoGenerar empleadoGenerar = new EmpleadoGenerar(); | |
Empleado empleadoVendedor = new Empleado(); | |
empleadoVendedor.setNombre("Gonzalo"); | |
empleadoVendedor.setApellidos("Fajardo Perez"); | |
empleadoVendedor.setCargo("Vendedor"); | |
empleadoVendedor.setSexo('M'); | |
empleadoVendedor.setFecha(new Date()); | |
empleadoVendedor.setDepartamento(null); | |
try { | |
empleadoGenerar.generarEmpleado(empleadoVendedor); | |
} catch (RuntimeException re) { | |
err.println("Ha ocurrido un RuntimeException!!"); | |
} | |
System.exit(0); | |
} | |
public static void mockDemoA() { | |
DepartamentoGenerar departamentoGenerar = new DepartamentoGenerar(); | |
Departamento departamentoVentas = new Departamento(); | |
departamentoVentas.setEmpleados(null); | |
departamentoVentas.setNombre("Ventas"); | |
departamentoVentas.setResponsable("Luis Alcantara"); | |
try { | |
departamentoGenerar.generarDepartamento(departamentoVentas); | |
} catch (NullPointerException npe) { | |
npe.printStackTrace(); | |
} catch (RuntimeException re) { | |
err.println("Ha ocurrio una RuntimeException!!"); | |
} | |
System.exit(0); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.codemonkey.test; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
import org.junit.After; | |
import org.junit.AfterClass; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
public class TestUnitJpa { | |
public static final String BD = "curso"; | |
public static EntityManagerFactory emf; | |
protected EntityManager em; | |
@BeforeClass | |
public static void setupStatic() { | |
Persistence.generateSchema(BD, null); | |
emf = Persistence.createEntityManagerFactory(BD); | |
} | |
@Before | |
public void setup() { | |
em = emf.createEntityManager(); | |
} | |
@After | |
public void tearDown() { | |
em.clear(); | |
em.close(); | |
} | |
@AfterClass | |
public static void tearDownStatic() { | |
emf.close(); | |
} | |
} |
Comentarios
Publicar un comentario