When I create a jar with dependencies from my project (Maven) and run it, the database (SQLite with Hibernate and SQLite Dialect For Hibernate) is stored in the same location as the jar. How can I change the location of the database ?
Snippet from pom.xml
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Starter</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
Snippet from hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!--SQLite settings-->
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="connection.url">jdbc:sqlite:Example.sqlite</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
CodePudding user response:
The connection url property in your hibernate.cfg.xml actually points to the database file. Since you did not specify any path, the current working directory will be used to store/access that file.
It is more a coincidence that you have your working directory the same as the directory holding all your jars.
That said, just put a different connection url and you are done. If you want to determine that at runtime, maybe do not use a hardcoded hibernate.cfg.xml file. You can for sure configure the hibernate session factory through code.