samedi 10 octobre 2015

How to use SQLite database with path relative to project/application?

My project uses the Maven build manager along with JPA and EclipseLink for interaction with a SQLite databse. The IDE of choice is NetBeans. This question however should not be anchored to the IDE but to the application that is developed in it.

I would like to make my application generate the SQLite database file that it will use in a path relative to the application's location. Is there a way to specify this inside the persistence.xml or do I have to manually code the procedure of creating the file?

Currently inside the <properties/> section of my persistence.xml I have (among other things):

<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:/home/user/NetBeansProjects/Cookbook/cookbook_db.sqlite"/>

I have tried for example to replace it with

<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:cookbook_db.sqlite"/>

This creates a database file inside my /home directory however I get permission error when I try to interact with it with my Java application. Entering an absolute path (as in the first example) resolves the problem. Is an absolute path really mandatory?

The behaviour I'm looking for is similar to - let's say - cmake's CMAKE_BINARY_DIR. Upon execution cmake extracts the path from this variable thus allowing the user to alter the binary directory at will. Maven itself supports relative paths in its pom.xml using the <relativePath/> tag.

Aucun commentaire:

Enregistrer un commentaire