Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

hibernate - EntityManager does not write to database

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...