gpt4 book ai didi

java - em.persist 似乎不会在 postgreSQL 数据库上保留数据

转载 作者:行者123 更新时间:2023-11-29 06:21:47 24 4
gpt4 key购买 nike

我有一个简单的 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>

感谢您的帮助。

马里奥

最佳答案

首先,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.

关于java - em.persist 似乎不会在 postgreSQL 数据库上保留数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2744335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com