Hibernate:记录到DB后无法获取数据

时间:2014-06-20 18:49:04

标签: java sql spring hibernate

我的无知中有愚蠢的错误,错误或漏洞。 可能是第三个。

解决: 来自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
...

因此,我创建了内存中的数据库,并在重新启动程序时使 autoclean。

我通常支持班级:

@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;
    }
}

通常是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>

主要课程:

    ...
    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());

执行它,输出为:

...
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 - 正确的数组大小

在此之后我改变了

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

仅在主类中执行

    ...
    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());

接收

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

为什么我得到0? 为什么新运行数据库清除后?

我无法打败它三个小时。 谢谢。

1 个答案:

答案 0 :(得分:1)

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

以上似乎意味着您使用嵌入式数据库。它并不持久,它会在您关闭时与您的应用程序一起消失。这种dbs最常用于测试目的。尝试一些像PostgreSQL或MySql这样的持久性。