简体   繁体   English

休眠:记录到数据库后无法获取数据

[英]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 已解决:来自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 通常是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 10-我的阵列大小正确

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? 为什么我得到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. 尝试一些持久性的,例如PostgreSQL或MySql。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法添加记录,Java,休眠 - cant add record, Java, Hibernate 数据库中的Hibernate Framework保存记录 - Hibernate Framework Saving Record in DB 为什么 Hibernate 无法创建数据库? SQLSyntaxErrorException 和 CommandAcceptanceException - Why Hibernate cant create DB? SQLSyntaxErrorException and CommandAcceptanceException 插入后,Hibernate从复合PK(DB中的序列)获取ID - Hibernate get ID from composite PK (sequence in DB) after insert Hibernate从Java中的db获取数据后,返回整数的null - Hibernate returns null for an integer after getting the data from db in Java 初始提交失败后,Hibernate会话不刷新数据库中的数据 - Hibernate session not refreshing data from DB after initial commit failure 在hibernate从DB检索数据后计算值 - Calculate values after hibernate retrieves data from DB 休眠:将数据插入数据库 - Hibernate: insert data into DB 在上一个事务失败后,hibernate / spring尝试插入而不是保存(db中的记录已存在) - hibernate/spring is trying to insert rather than save (record already exists in db) after the previous transaction failed 使用 findOne 和 findById 的 hibernate 看不到数据库中更新后触发器在记录上所做的更改 - Changes made on record by trigger after update in DB aren't visible by hibernate with findOne and findById
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM