Hi all .
It is the first time that i write on mailing list of OPEJPA .
I am movice to OpenJPA and i have a question.
I have writed a java SE app simple.
I have created a class Gestore.java .
my DAO is :
package gestoreentita;
import eentita.Persona;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
* @author utente_javaee7
*/
public class Gestore {
private static final Logger log =
Logger.getLogger(Gestore.class.getName());
EntityManagerFactory emf = null;
EntityManager em = null;
public Gestore() {
this.emf =
Persistence.createEntityManagerFactory("prova1openjpaallPU");
log.info("created the EntyManagerFactory " + emf.toString());
public EntityManager getEntityManager() {
if (this.em == null) {
em = emf.createEntityManager();
log.info("created the EntyManager " + em.toString());
return this.em;
public void closeEntytyManager() {
if (em.isOpen()) {
em.close();
em = null;
log.info("closed the EntyManager " + em.toString());
public void closeEntytyManagerFactory() {
if (emf.isOpen()) {
log.info("now i close the EntyManagerFactory " +
emf.toString());
emf.close();
log.info("the EntyManagerFactory now is :" + emf.toString());
public Persona savePersona(Persona newdetachedPerson) {
try {
em = this.getEntityManager();
em.getTransaction().begin();
em.persist(newdetachedPerson);
em.getTransaction().commit();
log.info("Persona è stata salvata in database con id =" +
newdetachedPerson.getId());
} catch (Exception e) {
em.getTransaction().rollback();
log.info("error into operation of persistence... Person not
saved!!!!");
} finally {
this.closetransaction();
this.closeEntytyManager();
return newdetachedPerson;
public List<Persona> getListPersona() {
List<Persona> lista = null;
try {
em = this.getEntityManager();
log.info("Creatop EntytyManager =" + em.toString());
log.info("Tentativo di ritrovare le enttita persona nel
database:");
lista = em.createQuery("select p from Persona
p").getResultList();
log.info("------ritrovate :" + lista.size());
} catch (Exception e) {
log.info("i have a Exception :" + e.getLocalizedMessage());
} finally {
this.closeEntytyManager();
return lista;
public void closetransaction() {
if (em.getTransaction().isActive()) {
em.close();
log.info("transaction closed!!!");
my application with class Main is:
package mauro.application;
import eentita.Persona;
import gestoreentita.Gestore;
import java.awt.HeadlessException;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
* @author utente_javaee7
*/
public class Main {
private static final Logger LOG =
Logger.getLogger(Main.class.getName());
public static Logger getLOG() {
return LOG;
private Gestore gestore;
public void closeEntityManagerFactory() {
this.gestore.closeEntytyManagerFactory();
public Main() {
this.gestore = new Gestore();
public static void main(String[] arg) {
Main main = new Main();
main.test1();
public void test1() {
try {
Persona p1, p2, p3, p4;
p1 = new Persona();
p1.setNome("pippo");
p2 = new Persona();
p2.setNome("pluto");
p3 = new Persona();
p3.setNome("paperino");
p4 = new Persona();
p4.setNome("archimede");
p1 = gestore.savePersona(p1);
JOptionPane.showMessageDialog(null, "Salvato: " + p1.getNome()
+ " con ID= : " + p1.getId());
p2 = gestore.savePersona(p2);
JOptionPane.showMessageDialog(null, "Salvato: " + p2.getNome()
+ " con ID= : " + p2.getId());
p3 = gestore.savePersona(p3);
JOptionPane.showMessageDialog(null, "Salvato: " + p3.getNome()
+ " con ID= : " + p3.getId());
p4 = gestore.savePersona(p4);
JOptionPane.showMessageDialog(null, "Salvato: " + p4.getNome()
+ " con ID= : " + p4.getId());
List<Persona> listaPersone = gestore.getListPersona();
JOptionPane.showMessageDialog(null, "into the db are present :
" + listaPersone.size() + " Persone");
} catch (Exception e) {
LOG.info("i have a exception: " + e.getLocalizedMessage());
} catch (Throwable t) {
LOG.info("i have a error: " + t.getLocalizedMessage());
} finally {
this.gestore.closeEntytyManagerFactory();
public Gestore getGestore() {
return gestore;
It is the first time that i write on mailing list of OPEJPA .
I am movice to OpenJPA and i have a question.
I have writed a java SE app simple.
I have created a class Gestore.java .
my DAO is :
package gestoreentita;
import eentita.Persona;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
* @author utente_javaee7
*/
public class Gestore {
private static final Logger log =
Logger.getLogger(Gestore.class.getName());
EntityManagerFactory emf = null;
EntityManager em = null;
public Gestore() {
this.emf =
Persistence.createEntityManagerFactory("prova1openjpaallPU");
log.info("created the EntyManagerFactory " + emf.toString());
public EntityManager getEntityManager() {
if (this.em == null) {
em = emf.createEntityManager();
log.info("created the EntyManager " + em.toString());
return this.em;
public void closeEntytyManager() {
if (em.isOpen()) {
em.close();
em = null;
log.info("closed the EntyManager " + em.toString());
public void closeEntytyManagerFactory() {
if (emf.isOpen()) {
log.info("now i close the EntyManagerFactory " +
emf.toString());
emf.close();
log.info("the EntyManagerFactory now is :" + emf.toString());
public Persona savePersona(Persona newdetachedPerson) {
try {
em = this.getEntityManager();
em.getTransaction().begin();
em.persist(newdetachedPerson);
em.getTransaction().commit();
log.info("Persona è stata salvata in database con id =" +
newdetachedPerson.getId());
} catch (Exception e) {
em.getTransaction().rollback();
log.info("error into operation of persistence... Person not
saved!!!!");
} finally {
this.closetransaction();
this.closeEntytyManager();
return newdetachedPerson;
public List<Persona> getListPersona() {
List<Persona> lista = null;
try {
em = this.getEntityManager();
log.info("Creatop EntytyManager =" + em.toString());
log.info("Tentativo di ritrovare le enttita persona nel
database:");
lista = em.createQuery("select p from Persona
p").getResultList();
log.info("------ritrovate :" + lista.size());
} catch (Exception e) {
log.info("i have a Exception :" + e.getLocalizedMessage());
} finally {
this.closeEntytyManager();
return lista;
public void closetransaction() {
if (em.getTransaction().isActive()) {
em.close();
log.info("transaction closed!!!");
my application with class Main is:
package mauro.application;
import eentita.Persona;
import gestoreentita.Gestore;
import java.awt.HeadlessException;
import java.util.List;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
* @author utente_javaee7
*/
public class Main {
private static final Logger LOG =
Logger.getLogger(Main.class.getName());
public static Logger getLOG() {
return LOG;
private Gestore gestore;
public void closeEntityManagerFactory() {
this.gestore.closeEntytyManagerFactory();
public Main() {
this.gestore = new Gestore();
public static void main(String[] arg) {
Main main = new Main();
main.test1();
public void test1() {
try {
Persona p1, p2, p3, p4;
p1 = new Persona();
p1.setNome("pippo");
p2 = new Persona();
p2.setNome("pluto");
p3 = new Persona();
p3.setNome("paperino");
p4 = new Persona();
p4.setNome("archimede");
p1 = gestore.savePersona(p1);
JOptionPane.showMessageDialog(null, "Salvato: " + p1.getNome()
+ " con ID= : " + p1.getId());
p2 = gestore.savePersona(p2);
JOptionPane.showMessageDialog(null, "Salvato: " + p2.getNome()
+ " con ID= : " + p2.getId());
p3 = gestore.savePersona(p3);
JOptionPane.showMessageDialog(null, "Salvato: " + p3.getNome()
+ " con ID= : " + p3.getId());
p4 = gestore.savePersona(p4);
JOptionPane.showMessageDialog(null, "Salvato: " + p4.getNome()
+ " con ID= : " + p4.getId());
List<Persona> listaPersone = gestore.getListPersona();
JOptionPane.showMessageDialog(null, "into the db are present :
" + listaPersone.size() + " Persone");
} catch (Exception e) {
LOG.info("i have a exception: " + e.getLocalizedMessage());
} catch (Throwable t) {
LOG.info("i have a error: " + t.getLocalizedMessage());
} finally {
this.gestore.closeEntytyManagerFactory();
public Gestore getGestore() {
return gestore;