我有一个简单的java main,它必须在PostgreSQL数据库上编写bean数据。我使用实体管理器来持久化或更新对象。我使用persistence.xml文件中指定的hibernate和toplink驱动程序连接。 当我调用em.persist(obj)时,数据库中没有保存任何内容,我不知道为什么。这是我的简单代码:
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);
这是我的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 name="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>
感谢您的帮助。
马里奥
答案 0 :(得分:2)
首先,persist
,merge
,remove
操作不会直接命中数据库,它们会在持久化上下文(事务)中更改内存中对象的状态。当事务为committed时,或者持久性上下文为flushed时,更改将写入数据库。
其次,persist
操作只能在事务中调用,异常将在事务之外抛出。所以你需要开始交易。
这是一个修改过的例子:
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.
答案 1 :(得分:1)
em.persist不会持久化该对象,它会将其标记为可持久化,并且在提交时该对象将被持久化。
我假设您的代码片段中没有提交?
EntityTransaction tx = em.getTransaction();
tx.begin();
dispositivoMedico = new DispositivoMedico(); dispositivoMedico.setCategoria("prova"); dispositivoMedico.setCodice("323");
em.persist(dispositivoMedico);
tx.commit();
em.close();