1

I have a persistence.xml file like this:

<?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>

And I'd like to change url path with replacing my username with the correct username (the one which is using the application).

I wrote an "Init" function which should override jdbc:url property

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);

where

DB_FILE_PATH = System.getProperty("user.home") + File.separator + "Library" + File.separator + "Application Support" + File.separator + "AppName" + File.separator + DB.SQLite;

but it doesn't work raising (long story short): /User/Dave folder not present, e.g. to simulate an installation in a pc with a user name different from mine I changed jdbc:url replacing "Dave" with (a random name) "Alice". So, System.getProperty("user.home") = "/Users/Dave" (which is the current user is running the application, but Hibernate is still looking for "/Users/Alice" ignoring override and raising:

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

Am I missing something? Any suggestion will be appreciate!

insilenzio
  • 918
  • 1
  • 9
  • 23

2 Answers2

1

Isn't javax.persistence.jdbc.url the correct property name?

thomas.mc.work
  • 6,404
  • 2
  • 26
  • 41
  • you're right: javax.persistence.jdbc.url is the correct property name, but still doesn't work. I've removed "javax.persistence." for testing and I forgot to replace when I posted the question... Sorry. I've edited the question. – insilenzio May 28 '15 at 10:42
1

I really don't know why, but swapping the second and third lines makes this code works! I wrongly supposed that the third line overwrite the persistanceMap entries with the entries and value in persistence.xml tagged as property, but in debug mode doesn't seem so!

Here is the corrected code:

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);
insilenzio
  • 918
  • 1
  • 9
  • 23