MisterD
MisterD

Reputation: 75

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?

Upvotes: 5

Views: 7433

Answers (3)

vkd
vkd

Reputation: 1

Hope this helps someone. The suggestion to set the write delay to 0 ms works, but it does not work from the url unless you're creating a new db. To have it apply, run it in a script while connected to the database using the sql statement below:

set files write delay false

So for example, you can run it using the sqltool.jar like so:

java -jar /path/to/sqltool.jar -sql "set files write delay false;" --inlinerc=<rc spec here>

Upvotes: 0

fredt
fredt

Reputation: 24372

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.

Upvotes: 5

axtavt
axtavt

Reputation: 242786

You need to add shutdown=true to the database URL (or issue an explict SHUTDOWN command) in order to correctly shut down an in-process HSQLDB database:

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

See also:

Upvotes: 3

Related Questions