Intellij JPA 控制台:为 H2 内存数据库配置 persistence.xml

Intellij JPA console: configure persistence.xml for H2 in-memory database

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

生成的申请-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 控制台时,所有查询均失败,声称未找到 tables。

例如

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 中的数据源。我现在可以在那里浏览 table 内容。 然后,我必须将数据源分配给 Intellij 持久性视图中的 entityManagerFactory。此外,我需要使用与 application.yml.

中相同的 NamingStrategie

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

中配置的不同

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

端口18080是基于web端口(如8080)+10000(见h2TCPServer()方法),在应用程序启动时记录为"H2 database is available on port xxxxx".

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

根据 M. Deinum 的建议,您应该删除 persistence.xml