EntityManager不写入数据库 [英] EntityManager does not write to database

查看:103
本文介绍了EntityManager不写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的课程是一个非常小的项目maven / jpa / hibernate项目,在那里我试图坚持一个对象。简单的一个:

  @Entity 
public class Person {
@Id @GeneratedValue
private int id;
私人字符串名称;
}

我的persistence.xml也非常基础:

 < persistence 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
version =2.0>
< persistence-unit name =Fahrplan_v2>
< class> model.Person< / class>
<属性>
< property name =hibernate.connection.driver_classvalue =org.hsqldb.jdbcDriver/>
< property name =hibernate.connection.usernamevalue =sa/>
< property name =hibernate.connection.passwordvalue =/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.HSQLDialect/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< / properties>
< / persistence-unit>
< /余辉>

最后这里是我用来保存对象的代码:

  EntityManager em = entityManagerFactory.createEntityManager(); 
em.getTransaction()。begin();
em.persist(person);
// em.flush(); < - 不影响结果。
em.getTransaction()。commit();
em.close();

现在我预计会发生两件事情:首先,我希望Person表被创建(由于hibernate.hbm2ddl.auto = update)。这发生过一次,它正确地写出了

  CREATE MEMORY TABLE PUBLIC.PERSON(
ID INTEGER GENERATED BY DEFAULT AS IDENTITY(从第1行开始)NOT NULL PRIMARY KEY,
NAME VARCHAR(255)

但我无法重现。每当我开始我的程序,hsqldb数据库文件被创建,但没有创建表。

第二,我期望持久化对象被存储在数据库中,但事实并非如此。另外,手动创建数据库模式并不能解决问题,所以这不是导致它的原因。持久化代码在输出中运行时没有任何异常或任何警告,它看起来非常好。但是这个对象并没有到达数据库。当使用from Person查询实体经理时,该对象也没有找到。



然而,查询似乎是唯一能够工作的东西。我可以手动将数据插入到数据库中,从Person查询将成功检索它。



那么,我在这里做错了什么提示?解析方法

添加到axtavt的良好答案中,并阐明您的睡眠方式(1000)是如何工作的:对于您希望完全同步持久性的开发情况,关闭默认的write_delay。

 < property name =hibernate.connection.url
value =jdbc :HSQLDB:文件:数据/的db / db;关闭= TRUE; hsqldb.write_delay_millis = 0\" />

这意味着每个语句都会在结果返回给调用者之前写入磁盘。自然地,在正常操作中,您可以增加此值。默认值为500毫秒,需要睡眠(1000)。给出的信息用于HSQLDB 2.2.x版。


i just set up a so far still quite minimal project maven/jpa/hibernate project, where i am trying to persist an object.

My class is a quite simple one:

@Entity
public class Person {
    @Id @GeneratedValue
    private int id;
    private String name;
}

My persistence.xml is very basic as well:

<persistence 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"
    version="2.0">
    <persistence-unit name="Fahrplan_v2">
        <class>model.Person</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.connection.url" value="jdbc:hsqldb:file:data/db/db" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

And lastly here's the code i use to persist the object:

EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
em.persist(person);
// em.flush(); <- does not effect outcome.
em.getTransaction().commit();
em.close();

Now there's two things i would expect to happen here: First, i'd expect the Person table to be created (due to hibernate.hbm2ddl.auto=update). This has happened once, and it correctly wrote out

CREATE MEMORY TABLE PUBLIC.PERSON(
    ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,
    NAME VARCHAR(255)
)

but i can't reproduce that at all. Every time i start my program, the hsqldb database files get created, but no tables are created.

Second, i would expect the persisted object to be stored in the database, but it is not. Also, manually creating the database schema doesn't solve the problem, so that's not what's causing it. The persisting code runs without any exceptions or any warnings in the output, it all looks perfectly fine. But the object just does not arrive in the database. The object is also not found when querying the entity manager with a "from Person".

The querying however seems to be the only thing that DOES work. i can manually insert a datum into the database and the "from Person" query will successfully retrieve it.

so, any hints on what i am doing wrong here?

解决方案

Adding to the good answer by axtavt and clarifying how your sleep(1000) worked: For development situations where you want absolutely synchronous persistence, turn off the default write_delay.

 <property name="hibernate.connection.url" 
      value="jdbc:hsqldb:file:data/db/db;shutdown=true;hsqldb.write_delay_millis=0"/>             

This means every statement is written to disk before the result is returned to the caller. Naturally, in normal operation, you can increase this value. The default is 500 ms, which needs the sleep(1000). The information given is for HSQLDB version 2.2.x.

这篇关于EntityManager不写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆