为什么我的数据没有得到持久化?
我在我的应用程序中使用 JPA 和 Java Embedded DB。我尝试将一些数据写入数据库,当我尝试读回它时,我能够做到这一点。但是应用程序已关闭,当我再次打开它时,没有任何数据存在。
这是我的 persistence.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myMoneyPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>mymoney.Money</class>
<properties>
<property name="eclipselink.jdbc.password" value="adminadmin"/>
<property name="eclipselink.jdbc.user" value="admin"/>
<property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="eclipselink.jdbc.url" value="jdbc:derby:pocketmoney;create=true"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
我的数据库 URL 是否有任何问题。
jdbc:derby:pocketmoney
I am using JPA and Java Embedded DB in my application. I try to write some data to the database and when i try to read it back I am able to do it. But the application is closed and when I open it again none of the data exists.
Here is my persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myMoneyPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>mymoney.Money</class>
<properties>
<property name="eclipselink.jdbc.password" value="adminadmin"/>
<property name="eclipselink.jdbc.user" value="admin"/>
<property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="eclipselink.jdbc.url" value="jdbc:derby:pocketmoney;create=true"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Is there any problem in my database URL.
jdbc:derby:pocketmoney
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,它是正确的,应该在当前工作目录中创建一个数据库。
调试问题的一些想法:
jdbc:derby:/path/to/pocketmoney;create=true
。使用以下属性激活 EcliseLink 日志记录以查看到底发生了什么:
<前><代码><属性>;
...
<属性名称=“eclipselink.debug”值=“全部”/>
<属性名称=“eclipselink.logging.level”值=“FINEST”/>
<属性名称=“eclipselink.logging.level.sql”值=“FINEST”/>
<属性名称=“eclipselink.logging.level.cache”值=“FINEST”/>
仔细检查您是否正确提交了事务(您说您的代码正在执行操作,但您没有显示它,所以仍然有一个疑问)。
参考文献
No, it is correct and should create a database in the current working directory.
Some ideas to debug your issue:
jdbc:derby:/path/to/pocketmoney;create=true
.Activate EcliseLink logging to see what is happening exactly using the following properties:
Double check that you do commit transactions appropriately (you are saying your code is doing things but you're not showing it so there is still a doubt).
References