应用程序运行后,Derby 数据库不保存实体
我将嵌入式 Derby DB 与 hibernate 一起使用。我正在将一些实体保存到数据库中。关闭应用程序后,数据库中没有实体。为什么会这样呢?
下面是我的 Hibernate 配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="connection.url">jdbc:derby:\bases\localbase;create=true</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<mapping class="com.example.model.HistoryItem"/>
<mapping class="com.example.model.User"/>
<mapping class="com.example.model.BaseAbstractEntity"/>
</session-factory>
</hibernate-configuration>
I'm using Embedded Derby DB with hibernate. I'm saving some entities to database. After shutting down the application there is no entities in DB. Why it could be so?
Below my Hibernate configuration
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="connection.url">jdbc:derby:\bases\localbase;create=true</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<mapping class="com.example.model.HistoryItem"/>
<mapping class="com.example.model.User"/>
<mapping class="com.example.model.BaseAbstractEntity"/>
</session-factory>
</hibernate-configuration>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很奇怪,因为默认情况下,Derby 在每次提交后都会同步到磁盘(除非您设置属性 derby.system.durability=test)。你的网址看起来是正确的(尽管我会使用正斜杠)。
这就引出了一个问题:您如何管理交易?你确定你犯了这些罪吗?
This is weird because Derby sync to the disk after each commit by default (unless you set the property derby.system.durability=test). And your url looks correct (although I would use forward slashes).
This begs the question: how do you managed transactions? Are you sure that you are committing them?
Derby 喜欢保留数据 - 即将其保留在内存中。如果您尝试通过以下方式关闭它:
这应该确保数据刷新到磁盘并且 derby 干净地关闭。
目前我正在尝试让 Hibernate 3.6 为我做这件事 - 到目前为止还没有运气。
Derby likes to hang on to the data - ie keep it in memory. If you try shutting it down by trying something like:
This should make sure the data is flushed to the disk and derby is shut down cleanly.
At the moment I am trying to get Hibernate 3.6 to do this for me - no luck so far.
也许您找错了地方。也许 Hibernate 应用程序正在硬盘上的一个位置创建 Derby 数据库,但是当您稍后查找数据库内容时,您将在硬盘上的不同位置查找。由于您的 Derby URL 表示“create=true”,如果您没有获得与原始应用程序中完全相同的位置规范,Derby 将悄悄地为您创建一个新的空数据库。
Perhaps you are looking in the wrong place. Perhaps the Hibernate application is creating a Derby database in one location on your hard disk, but when you look for the database contents later, you are looking in a different location on your hard disk. Since your Derby URL says "create=true", Derby will quietly create a new empty database for you if you don't get exactly the same location specification as in your original app.