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

标签 java hibernate jpa hikaricp guice-persist

我有一个使用 GuicePersist、Hibernate 和 HikariCP 与 Postgres 数据库通信的 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");

关于java - 在运行时配置 HikariCP + Hibernate + GuicePersist(JPA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30920073/

相关文章:

java - 从 LDAP groupOfUniqueNames 中提取多值属性

java - 动态更改@ManagedResource objectName

java - hibernate 当前日期不工作

java - EclipseLink 错误 : Exception Description: Missing descriptor for class

java - JPA 鉴别器值

java - Hibernate一对一关系和外键问题

java - "JSchException: reject HostKey"使用 JSch 从 Java 连接到 SSH 服务器时

java - 是否有更有效的方法将 JTable 中已编辑的单元格内容重置回之前的内容

java - 在每个具体类的一对一表结构中创建外键

mysql - 无法使用 Spring Boot Hibernate 和 MySQL 存储 "Pile of Poo"unicode 表情符号