I've got a simple java main which must write bean data on a PostgreSQL database. I use Entity manager to persist or update object. I use hibernate and toplink driver connection which are specified in persistence.xml file. When I call em.persist(obj), nothing is saved on database, I don't know why. here is my simple code:
private static void importa(FileReader f) throws IOException {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("orpt2");
EntityManager em = emf.createEntityManager();
dispositivoMedico = new DispositivoMedico();
dispositivoMedico.setCategoria("prova");
dispositivoMedico.setCodice("323");
em.persist(dispositivoMedico);
And here is my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit na开发者_运维百科me="orpt2">
<class>it.ariadne.orpt2.entities.AccessoriScheda</class>
<class>it.ariadne.orpt2.entities.CampiSchede</class>
<class>it.ariadne.orpt2.entities.CampiSchedeSalvati</class>
<class>it.ariadne.orpt2.entities.CampoAggiuntivo</class>
<class>it.ariadne.orpt2.entities.Categorie</class>
<class>it.ariadne.orpt2.entities.CategorieCampi</class>
<class>it.ariadne.orpt2.entities.CategorieCampiPK</class>
<class>it.ariadne.orpt2.entities.ClasseCivab</class>
<class>it.ariadne.orpt2.entities.DecodificaStato</class>
<class>it.ariadne.orpt2.entities.DispositivoMedico</class>
<class>it.ariadne.orpt2.entities.Ente</class>
<class>it.ariadne.orpt2.entities.FormaNegoziazione</class>
<class>it.ariadne.orpt2.entities.Fornitore</class>
<class>it.ariadne.orpt2.entities.LogSession</class>
<class>it.ariadne.orpt2.entities.Modello</class>
<class>it.ariadne.orpt2.entities.Periodicita</class>
<class>it.ariadne.orpt2.entities.Produttore</class>
<class>it.ariadne.orpt2.entities.Ruolo</class>
<class>it.ariadne.orpt2.entities.RuoloPK</class>
<class>it.ariadne.orpt2.entities.RuoloUtente</class>
<class>it.ariadne.orpt2.entities.Scheda</class>
<class>it.ariadne.orpt2.entities.SchedaSalvata</class>
<class>it.ariadne.orpt2.entities.Tipologia</class>
<class>it.ariadne.orpt2.entities.Utente</class>
<!-- locale 2010--><!-- optsanmatteo_prova300310 -->
<properties>
<property name="hibernate.connection.driver_class"
value="org.postgresql.Driver" />
<property name="hibernate.connection.url"
value="jdbc:postgresql://localhost:5432/optsanmatteo_provaHash" />
<property name="hibernate.connection.password"
value="s4sh4gr3y" />
<property name="hibernate.connection.username"
value="sanmatteo" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="toplink.logging.level" value="WARNING" />
<property name="toplink.jdbc.driver"
value="org.postgresql.Driver" />
<property name="toplink.jdbc.url"
value="jdbc:postgresql://localhost:5432/optsanmatteo_provaHash" />
<property name="toplink.jdbc.password" value="s4sh4gr3y" />
<property name="toplink.jdbc.user"
value="sanmatteo" />
</properties>
</persistence-unit>
</persistence>
Thank you for your help.
Mario
First, the persist
, merge
, remove
operations won't hit the database directly, they change the state of objects in memory - in the persistence context (the transaction). When the transaction is committed, or if the persistence context is flushed, then the changes are written to the database.
Second, the persist
operation can only be called within a transaction, an exception will be thrown outside of a transaction. So you need to start a transaction.
Here is a modified example:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("orpt2");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin(); // start a transaction
dispositivoMedico = new DispositivoMedico();
dispositivoMedico.setCategoria("prova");
dispositivoMedico.setCodice("323");
em.persist(dispositivoMedico);
em.getTransaction().commit(); // Commit the current resource transaction, writing
// any unflushed changes to the database.
em.persist doesn't persist the object, it marks it as persistable and at the commit the object will be persisted.
I assume from your code fragment you don't have a commit?
EntityTransaction tx = em.getTransaction();
tx.begin();
dispositivoMedico = new DispositivoMedico(); dispositivoMedico.setCategoria("prova"); dispositivoMedico.setCodice("323");
em.persist(dispositivoMedico);
tx.commit();
em.close();
精彩评论