在运行时配置 HikariCP + Hibernate + GuicePersist(JPA)

Configure HikariCP + Hibernate + GuicePersist(JPA) at Runtime

我有一个 java8 桌面应用程序,它使用 GuicePersist、Hibernate 和 HikariCP 与 Postgres 数据库进行通信。我已经成功地使用 META-INF/persistence.xml:

将我的应用 send/receive 数据传输到数据库
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <!-- A JPA Persistence Unit -->
    <persistence-unit name="myJPAunit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.123fakestreet.bogus.tomb.impl.postgres.model.movies</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <!-- SQL stuff -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />

            <!-- hikari CP -->
            <property name="hibernate.connection.provider_class" value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider" />
            <property name="hibernate.hikari.minimumIdle" value="20" />
            <property name="hibernate.hikari.maximumPoolSize" value="100" />
            <property name="hibernate.hikari.idleTimeout" value="30000" />
            <property name="hibernate.hikari.dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource" />
            <property name="hibernate.hikari.dataSource.url" value="jdbc:postgresql://192.168.100.75:5432/mpDb" />
            <property name="hibernate.hikari.username" value="cowboy" />
            <property name="hibernate.hikari.password" value="bebop" />

            <!-- Disable the second-level cache  -->
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>

            <!-- Default is false for backwards compatibility.  Should be used on all new projects -->
            <property name="hibernate.id.new_generator_mappings" value="true"/>

        </properties>
    </persistence-unit>
</persistence>

棘手的部分是在运行时配置我的 Guice JpaPersistModule。 据我所知,我应该能够通过在 Map 对象中设置属性来覆盖 META-INF/persistence.xml 文件中的属性,如下所示:

Map<String, String> properties = new HashMap<>();
    properties.put("myJPAunit.hibernate.hikari.dataSource.url", "jdbc:postgresql://192.168.100.75:5432/mpDb");
    properties.put("myJPAunit.hibernate.hikari.dataSource.user", "cowboy");
    properties.put("myJPAunit.hibernate.hikari.dataSource.password", "bebop");

然后jpaModule.properties(属性),然后将这一切传递给 GuicePersist。

我已经在地图中尝试了 属性 名称的一些不同组合,但到目前为止我还没有成功。我还查看了关于此主题的 pgsimpledatasource 文档和 hikariCP 文档,但我的数据源属性仍然没有设置。

有人可以帮忙吗?非常感谢。

尝试从 JPA 属性中删除持久性单元名称,而不是:

Map<String, String> properties = new HashMap<>();
properties.put("myJPAunit.hibernate.hikari.dataSource.url", "jdbc:postgresql://192.168.100.75:5432/mpDb");
properties.put( + "myJPAunit.hibernate.hikari.dataSource.user", "cowboy");
properties.put( + "myJPAunit.hibernate.hikari.dataSource.password", "bebop");

你应该有这个:

Map<String, String> properties = new HashMap<>();
properties.put("hibernate.hikari.dataSource.url", "jdbc:postgresql://192.168.100.75:5432/mpDb");
properties.put("hibernate.hikari.dataSource.user", "cowboy");
properties.put("hibernate.hikari.dataSource.password", "bebop");