我有一个使用GuicePersist,Hibernate和HikariCP与Postgres DB通信的java8桌面应用程序。我已经成功使用此META-INF/persistence.xml使我的应用程序向数据库发送/接收数据:

<?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(properties),然后将其全部传递给GuicePersist。

我在 map 中尝试了几种不同的属性名称组合,但到目前为止我还没有运气。我也查看了有关该主题的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");

09-10 02:08