小编Mis*_*erD的帖子

EntityManager不会写入数据库

我刚刚设置了一个目前为止仍然非常小的项目maven/jpa/hibernate项目,我试图坚持一个对象.

我的课很简单:

@Entity
public class Person {
    @Id @GeneratedValue
    private int id;
    private String name;
}
Run Code Online (Sandbox Code Playgroud)

我的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>
        <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>
Run Code Online (Sandbox Code Playgroud)

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

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

现在我希望在这里发生两件事:首先,我希望创建Person表(由于hibernate.hbm2ddl.auto = update).这已经发生了一次,它正确地写出来了

CREATE MEMORY TABLE PUBLIC.PERSON(
    ID INTEGER GENERATED …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa hsqldb entitymanager

5
推荐指数
1
解决办法
6386
查看次数

标签 统计

entitymanager ×1

hibernate ×1

hsqldb ×1

jpa ×1