Java HSQLDB 和 Hibernate/JPA - 不持久化到磁盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3551988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HSQLDB and Hibernate/JPA - not persisting to disk?
提问by HenryR
Something of an novice with HSQL and Hibernate...
HSQL 和 Hibernate 的新手......
em.getTransaction().begin();
for (Activity theActivity : activities) {
em.persist(theActivity);
}
em.getTransaction().commit();
em.close();
followed by...
其次是...
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
System.out.println("QUERY:: "
+ em.createQuery("SELECT COUNT(*) FROM " + Activity.class.getName()).getSingleResult()
.toString());
em.getTransaction().commit();
Prints 25000 (the number of Activity objects in activities). But when I run this test again, the number of objects in the count(*) doesn't increase (and is 0 at the beginning of the program). So the objects aren't getting durably written.
打印 25000(活动中的活动对象数)。但是当我再次运行这个测试时,count(*) 中的对象数量并没有增加(并且在程序开始时为 0)。所以对象没有得到持久的写入。
This is my hsqldb connection string:
这是我的 hsqldb 连接字符串:
name="hibernate.connection.url" value="jdbc:hsqldb:file:data/cmon"
so it's not an in-memory database as far as I know...
所以据我所知,它不是内存数据库......
Does anyone have any ideas why the objects aren't getting persisted beyond a single JVM session? Happy to supply more information but there's so much state associated with Hibernate / JPA / HSQL that it's not clear exactly what is pertinent.
有没有人知道为什么对象没有在单个 JVM 会话之外持久化?很高兴提供更多信息,但是与 Hibernate/JPA/HSQL 相关的状态太多了,不清楚到底什么是相关的。
采纳答案by Pascal Thivent
Does anyone have any ideas why the objects aren't getting persisted beyond a single JVM session?
有没有人知道为什么对象没有在单个 JVM 会话之外持久化?
HSQLDB doesn't write changes immediately to disk after a commit (see "WRITE DELAY"), HSQLDB is not Durable by default (that's from where "performances" are coming from).
HSQLDB 不会在提交后立即将更改写入磁盘(请参阅“ WRITE DELAY”),默认情况下 HSQLDB 不是 Durable 的(这就是“性能”的来源)。
Either try to set the connection property shutdown=true
in the connection string to get the changes written when the last connection will end.
尝试shutdown=true
在连接字符串中设置连接属性以在最后一个连接结束时写入更改。
jdbc:hsqldb:file:data/cmon;shutdown=true
If it doesn't help, try to set the WRITE DELAYto 0 (or false). If you're using HSQLDB 1.8.x, use the SQL command:
如果没有帮助,请尝试将WRITE DELAY设置为 0(或 false)。如果您使用的是 HSQLDB 1.8.x,请使用 SQL 命令:
SET WRITE_DELAY 0
If you're using HSQLDB 2.0.x, you can now also use a connection propertyhsqldb.write_delay
:
如果您使用的是 HSQLDB 2.0.x,您现在还可以使用连接属性hsqldb.write_delay
:
jdbc:hsqldb:file:data/cmon;hsqldb.write_delay=false
回答by Chris Lercher
Did you set hibernate.hbm2ddl.auto
to create-drop
in your persistence.xml? This drops your tables and re-creates them on every startup.
你在你的persistence.xml中设置hibernate.hbm2ddl.auto
了create-drop
吗?这会删除您的表并在每次启动时重新创建它们。
You can set it to update
instead, or if you want to manage the schema yourself, then set it to validate
.
您可以将其设置为update
,或者如果您想自己管理架构,则将其设置为validate
。
回答by Maciek Kreft
The solution is :
解决办法是:
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
in hibernate.cfg.xml
在 hibernate.cfg.xml 中
This is rest of my configuration:
这是我的其余配置:
Libs:
库:
- HsqlDb 2.0.0
- Hibernate 3.5.6
- 数据库 2.0.0
- 休眠 3.5.6
Url:
网址:
<property name="connection.url">jdbc:hsqldb:file:data/mydb;shutdown=true;hsqldb.write_delay=false;</property>
回答by Jean-Luc
Simply close your EntityManagerFactory with HSQL in filemode, after the commit to really persist datas...
在提交真正持久化数据后,只需在文件模式下使用 HSQL 关闭 EntityManagerFactory...
回答by Kaushik Lele
I was using HSQL DB version 2.2.5. I tried above approaches i.e. setting shutdown=true and hsqldb.write_delay=false It did not work. As suggested in some blog, I added statement
我使用的是 HSQL DB 2.2.5 版。我尝试了上面的方法,即设置 shutdown=true 和 hsqldb.write_delay=false 它没有用。正如一些博客中所建议的,我添加了声明
org.hsqldb.DatabaseManager.closeDatabases(0);
after transaction commit. But it did not work.
事务提交后。但它没有用。
HSQL DB version 2.2.9 seems better than this. With one workaround it solves this problem. To handle above problem take following steps :-
HSQL DB 2.2.9 版似乎比这更好。通过一种解决方法,它解决了这个问题。要处理上述问题,请执行以下步骤:-
1) hsqldb.jar from lib of HSQL DB version 2.2.9
1) 来自 HSQL DB 版本 2.2.9 的 lib 的 hsqldb.jar
2) In hibernate config xml just specify URL I am using HSQL file-based database.
2) 在 hibernate config xml 中只指定 URL 我使用的是基于 HSQL 文件的数据库。
<property name="hibernate.connection.url">jdbc:hsqldb:file:D:\JavaProj\TmpDBLocation\myKauDB</property>
3) In your program at the end write statement
3)在你的程序末尾写语句
org.hsqldb.DatabaseManager.closeDatabases(0);
Now run the hibernate program that commits the data to DB.
现在运行将数据提交到 DB 的休眠程序。
Check HSQL DB by opening it in standalone mode and with URL
通过以独立模式和 URL 打开 HSQL DB 来检查它
jdbc:hsqldb:file:D:\JavaProj\TmpDBLocation\myKauDB
You should see your changes persisted in DB.
您应该会看到您的更改保留在 DB 中。
回答by Pradeep
Closing sessionfactoryworked for me.
关闭sessionfactory对我有用。