【问题标题】:Intellij JPA console: configure persistence.xml for H2 in-memory databaseIntellij JPA 控制台:为 H2 内存数据库配置 persistence.xml
【发布时间】:2019-03-04 15:46:00
【问题描述】:

我正在尝试使用 Intellij Idea Ultimate 中的 JPA 控制台来测试查询。该项目使用 JHipster 5.7.0 生成,并使用带有 Hazelcast 的 H2 内存数据库。

生成的 application-dev.yml:

...
datasource:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:h2:mem:appointmentservice;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
        username: appointmentservice
        password:
        hikari:
            auto-commit: false
    h2:
        console:
            enabled: true
    jpa:
        database-platform: io.github.jhipster.domain.util.FixedH2Dialect
        database: H2
        show-sql: false
        properties:
            hibernate.id.new_generator_mappings: true
            hibernate.connection.provider_disables_autocommit: true
            hibernate.cache.use_second_level_cache: true
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: true
            hibernate.cache.region.factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactory
            hibernate.cache.hazelcast.instance_name: appointmentservice
            hibernate.cache.use_minimal_puts: true
            hibernate.cache.hazelcast.use_lite_member: true
...

我在资源目录中创建了以下 persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="appointmentservice" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:appointmentservice"/>
            <property name="javax.persistence.jdbc.user" value="appointmentservice"/>
            <property name="hibernate.id.new_generator_mappings" value="true"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="hibernate.cache.use_query_cache" value="false"/>
            <property name="hibernate.generate_statistics" value="true"/>
            <property name="hibernate.cache.region.factory_class" value="com.hazelcast.hibernate.HazelcastCacheRegionFactory"/>
            <property name="hibernate.cache.hazelcast.instance_name" value="appointmentservice"/>
            <property name="hibernate.cache.use_minimal_puts" value="true"/>
            <property name="hibernate.cache.hazelcast.use_lite_member" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

以下配置适用于 H2 Web 控制台:

问题: 在 Intellij 持久性视图中,当单击约会服务时,所有实体都会显示。但是,当我右键单击它并打开 JPA 控制台时,所有查询都声称未找到表。

例如

jpa-ql> select a from Address a
[2019-03-04 16:03:57] [42S02] Table "ADDRESS" not found; SQL statement:
[2019-03-04 16:03:57] select address0_.id as id1_1_, address0_.active as active2_1_, address0_.city as city3_1_, address0_.clientAccount_id as clientAc9_1_, address0_.country as country4_1_, address0_.institution_id as institu10_1_, address0_.location_id as locatio11_1_, address0_.jhi_number as jhi_numb5_1_, address0_.street as street6_1_, address0_.supplement as suppleme7_1_, address0_.zip as zip8_1_ from address address0_ [42102-197]

如果有人能给我提示我做错了什么,或者是否有任何适合我的案例的好示例 persistence.xml 文件,我将不胜感激。 提前致谢

编辑: 感谢所有的回复! - 我按照@Gaël Marziou 的建议删除了 persistence.xml 并使用 tcp URL 连接到 Intellij 中的数据源。我现在可以在那里浏览表格内容。 然后我必须将数据源分配给 Intellij 持久性视图中的 entityManagerFactory。此外,我需要使用与 application.yml 中相同的 NamingStrategie。

【问题讨论】:

  • 这里没有错。但是,您正在使用仅对正在运行的进程可见的内存数据库。它不能从外部访问。因此,您不会在 Intellij 中看到相同的数据库。
  • 所以这也意味着即使应用程序运行我也无法访问它?
  • 否,除非您使用 tCP 端口配置它(如下面的答案所述)。或者只是不覆盖该属性。我还建议放弃persistence.xml,转而使用application.properties

标签: spring hibernate jpa intellij-idea jhipster


【解决方案1】:

JHipster 使用 TCP 端口创建 H2 服务器(请参阅 DatabaseConfiguration.java 中的 h2TCPServer() 方法),因此您的内存数据库可以从外部客户端使用 tcp JDBC url 访问,该 tcp JDBC url 与您在application.yml.

外部客户端应该使用jdbc:h2:tcp://localhost:18080/mem:appointmentservice

端口 18080 基于 web 端口(例如 8080)+ 10000(参见h2TCPServer() 方法),并在应用程序启动时记录为“H2 数据库在端口 xxxxx 上可用”。

我个人使用 DBeaver 在我的 JHipster 应用程序中访问 H2 数据库。

按照 M. Deinum 的建议,您应该删除 persistence.xml

【讨论】:

  • 如果他会使用默认的,但他重新配置了 URL。
  • 什么意思?我看不到 JHipster 生成的内容有任何变化。只要他不修改 JHipster 生成的 DatabaseConfiguration 类,我的提示应该可以工作。
  • 他更改了属性文件中的 URL 并且具有优先权。
  • 不,他没有改变它,它是生成的,它与我所描述的效果很好。
  • 是的,他做到了。查看他发布的applicaiton.yamlpersistence.xml。那显然有一个不同的 URL。此外,服务器仅在开发配置文件处于活动状态时才启动,而不是默认情况下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
相关资源
最近更新 更多