h2嵌入式数据库,javafx,hibernate

问题描述 投票:0回答:1

您好,我正在使用 Javafx、嵌入式 h2 数据库和 hibernate 创建一个桌面应用程序,以在它们之间建立连接。我的应用程序在桌面上运行良好,但当我在其他系统上运行可执行 jar 文件时,它无法从数据库获取数据,这意味着我的 db 文件未嵌入应用程序 jar 文件中。我的数据库文件位于根目录中。请告诉我如何将嵌入式 h2 数据库文件放入该可执行 jar 文件中,或者我应该在 hibernate.cfg 中进行哪些更改以将我的 test.h2.db 文件放入 app.jar 中。

My hibernate.cfg file is 
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">org.h2.Driver</property>
    <property name="hibernate.connection.url">jdbc:h2:~/test</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password"/>
    <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">
            org.hibernate.cache.NoCacheProvider
        </property>
    <property name="hbm2ddl.auto">update</property>
    <property name="show_sql">true</property>
    <mapping class="model.Employee"/>
  </session-factory>
</hibernate-configuration>
java hibernate javafx h2
1个回答
0
投票

嵌入式数据库并不意味着可以在不同的计算机上工作,它是本地数据库并且不共享,实现共享此数据库的唯一方法是使用远程版本数据库,例如MySQL(在服务器上工作)而不是嵌入式数据库幸运的是,H2数据库有远程版本,它可以是嵌入式、远程或内存数据库。 所以改变这个

<property name="hibernate.connection.url">jdbc:h2:~/test</property>

到此

<property name="hibernate.connection.url">jdbc:h2:tcp://localhost:9092/test</property>
© www.soinside.com 2019 - 2024. All rights reserved.