简体   繁体   中英

Hibernate: cant get data after record to DB

I have stupid error, or bug, or hole in my ignorance. Probably third.

SOLVED: From http://www.h2database.com/html/cheatSheet.html

Embedded
jdbc:h2:~/test 'test' in the user home directory
...
**In-Memory**
jdbc:h2:mem:test multiple connections in one process
...

So, I create in-memory DB, and it make autoclean when you restart the program.

I have typically support class:

@Entity
public class UserDetalis {
    @Id  @GeneratedValue(strategy = GenerationType.AUTO)
    private int userId;
    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
}

Typically hibernate.cfg.xml

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">org.h2.Driver</property>
    <property name="connection.url">jdbc:h2:mem:db1</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"></property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">10</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.H2Dialect</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>


    <mapping class="org.zyxerdima.dto.UserDetalis" />

</session-factory>

Main class:

    ...
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    for (int i = 0; i < 10; i++){
        UserDetalis user = new UserDetalis();
        user.setUserName("User " + (i + 1));
        session.save(user);
    }
    session.getTransaction().commit();
    session.close();

    session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.createQuery("from UserDetalis");
    List users = query.list();
    session.getTransaction().commit();
    session.close();
    System.out.println(users.size());

Execute it, and output is:

...
10x Hibernate: insert into UserDetalis (userId, userName) values (null, ?)
Hibernate: select userdetali0_.userId as userId1_0_, userdetali0_.userName as userName2_0_ from UserDetalis userdetali0_
10 

10 - correct size of my array

After this I change

<property name="hbm2ddl.auto">update</property>

And execute in main class only

    ...
    session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.createQuery("from UserDetalis");
    List users = query.list();
    session.getTransaction().commit();
    session.close();
    System.out.println(users.size());

And receive

Hibernate: select userdetali0_.userId as userId1_0_, userdetali0_.userName as userName2_0_ from UserDetalis userdetali0_
0

Why I get 0? Why after new run DB is clear?

I can not defeat it for three hours. Thank you.

<property name="connection.url">jdbc:h2:mem:db1</property>

Above seems to mean that you use an embedded database. It is not persistent, it dies together with your app when it's shut down. Such dbs are most commonly used for testing purposes. Try some persistent one like PostgreSQL or MySql.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM