[英]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.