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; } } --------------------------------------------------------------------------------------------------------------- my persitence.xml <?xml version="1.0" encoding="UTF-8"?> <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="prova1openjpaallPU" transaction-type="RESOURCE_LOCAL"> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> <class>eentita.Persona</class> <properties> <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/OneTOMAnyUnidirectionalWihJoinForeighKeyDB"/> <property name="javax.persistence.jdbc.password" value="app"/> <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/> <property name="javax.persistence.jdbc.user" value="app"/> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction='add,deleteTableContents',ForeignKeys=true)"/> <property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/> <property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=80, PrintParameters=True"/> </properties> </persistence-unit> </persistence> -------------------------------------------------------- the entity Persona: package eentita; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * * @author utente_javaee7 */ @Entity public class Persona implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } private String nome; @Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; } @Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if (!(object instanceof Persona)) { return false; } Persona other = (Persona) object; if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) { return false; } return true; } @Override public String toString() { return "eentita.Persona[ id=" + id + " ]"; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } } ---------------------------------- from the consolle i get: run: 1183 prova1openjpaallPU INFO [main] openjpa.Runtime - OpenJPA dynamically loaded the class enhancer. Any classes that were not enhanced at build time will be enhanced when they are loaded by the JVM. SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 1557 prova1openjpaallPU INFO [main] openjpa.Runtime - OpenJPA dynamically loaded a validation provider. feb 25, 2014 6:17:15 PM gestoreentita.Gestore <init> INFO: created the EntyManagerFactory org.apache.openjpa.persistence.EntityManagerFactoryImpl@106ff81 2715 prova1openjpaallPU INFO [main] openjpa.Runtime - Starting OpenJPA 2.2.2 4507 prova1openjpaallPU TRACE [main] openjpa.jdbc.SQL - <t 11800934, conn 15888232> executing stmnt 15418919 DELETE FROM Persona 4528 prova1openjpaallPU TRACE [main] openjpa.jdbc.SQL - <t 11800934, conn 15888232> [21 ms] spent feb 25, 2014 6:17:18 PM gestoreentita.Gestore getEntityManager INFO: created the EntyManager org.apache.openjpa.persistence.EntityManagerImpl@15beddd 4926 prova1openjpaallPU TRACE [main] openjpa.jdbc.SQL - <t 11800934, conn 1409906> executing prepstmnt 8994380 INSERT INTO Persona (nome) VALUES (?) [params=(String) pippo] 4933 prova1openjpaallPU TRACE [main] openjpa.jdbc.SQL - <t 11800934, conn 1409906> [7 ms] spent 4937 prova1openjpaallPU TRACE [main] openjpa.jdbc.SQL - <t 11800934, conn 1409906> executing prepstmnt 6876392 VALUES(IDENTITY_VAL_LOCAL()) 4947 prova1openjpaallPU TRACE [main] openjpa.jdbc.SQL - <t 11800934, conn 1409906> [2 ms] spent feb 25, 2014 6:17:18 PM gestoreentita.Gestore savePersona INFO: Persona è stata salvata in database con id =18 feb 25, 2014 6:17:18 PM mauro.application.Main test1 INFO: i have a exception: null feb 25, 2014 6:17:18 PM gestoreentita.Gestore closeEntytyManagerFactory INFO: now i close the EntyManagerFactory org.apache.openjpa.persistence.EntityManagerFactoryImpl@106ff81 feb 25, 2014 6:17:18 PM gestoreentita.Gestore closeEntytyManagerFactory INFO: the EntyManagerFactory now is :org.apache.openjpa.persistence.EntityManagerFactoryImpl@106ff81 BUILD SUCCESSFUL (total time: 8 seconds) for each methods i create a new EntityManager with the method getEntityManager() . It create a new EntityManager from the EMF and set into em field of class and retrun it . At each method i close the em for get a new em for other operation on DB . so i have created into another class named Main.java a test: i would insert 4 instances of entity Persona into db . But after the first insert i get a null .and a Exception. So i ask: whi the new entityManager is not created ??? Where it is the error into my code? Tank you Mauro