當我從我的專案(Maven)中創建一個具有依賴項的 jar 并運行它時,資料庫(帶有 Hibernate 和SQLite Dialect For Hibernate的 SQLite )存盤在與 jar 相同的位置。如何更改資料庫的位置?
來自 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>
來自 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>
uj5u.com熱心網友回復:
hibernate.cfg.xml 中的連接 url 屬性實際上指向資料庫檔案。由于您沒有指定任何路徑,因此當前作業目錄將用于存盤/訪問該檔案。
更巧合的是,您的作業目錄與保存所有 jar 的目錄相同。
也就是說,只需輸入一個不同的連接 url 就可以了。如果您想在運行時確定,也許不要使用硬編碼的 hibernate.cfg.xml 檔案。您肯定可以通過代碼配置休眠會話工廠。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448513.html
