我有一个像这样的persistence.xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="AppPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>util.Pazienti</class>
<class>util.Trattamenti</class>
<class>util.Pagamenti</class>
<class>util.Configurazioni</class>
<class>util.Modpagamenti</class>
<class>ent.Modpagamenti</class>
<class>ent.Configurazioni</class>
<class>ent.Trattamenti</class>
<class>ent.Pagamenti</class>
<class>ent.Pazienti</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:/Users/Alice/Library/Application Support/AppName/DB.SQLite"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC"/>
</properties>
</persistence-unit>
</persistence>
我想用正确的用户名(使用该应用程序的用户名)替换我的用户名来更改网址路径。
我写了一个&#34; Init&#34;应覆盖jdbc:url属性
的函数Map<String, String> persistenceMap = new HashMap<>();
persistenceMap.put("javax.persistence.jdbc.url", "jdbc:sqlite:"+DB_FILE_PATH);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("AppPU", persistenceMap);
emf.createEntityManager(persistenceMap);
,其中
DB_FILE_PATH = System.getProperty("user.home") + File.separator + "Library" + File.separator + "Application Support" + File.separator + "AppName" + File.separator + DB.SQLite;
但它没有工作(长话短说):/User/Dave folder not present
,例如模拟用户名不同于我的电脑中的安装我更改了jdbc:url replacement&#34; Dave&#34; (随机名称)&#34; Alice&#34;。
所以,System.getProperty("user.home") = "/Users/Dave"
(当前用户正在运行该应用程序,但Hibernate仍在寻找忽略覆盖和提升的"/Users/Alice"
:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: path to '/Users/Alice/Library/Application Support/AppName/DB.SQLite': '/Users/Alice' does not exist
Error Code: 0
Java Result: 255
我错过了什么吗?任何建议都将不胜感激!
答案 0 :(得分:1)
javax.persistence.jdbc.url
不是正确的属性名称吗?
答案 1 :(得分:1)
我真的不知道为什么,但是交换第二行和第三行会使这段代码有效!
我错误地认为第三行覆盖persistanceMap
条目,其中persistence.xml中的条目和值标记为property
,但在调试模式下似乎不是这样!
以下是更正后的代码:
Map<String, String> persistenceMap = new HashMap<>();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("AppPU", persistenceMap);
persistenceMap.put("javax.persistence.jdbc.url", "jdbc:sqlite:"+DB_FILE_PATH);
emf.createEntityManager(persistenceMap);