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

问题描述 投票:0回答:1

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

生成的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控制台:enter image description here

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

EG

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ëlMarziou的建议删除了persistence.xml,并使用tcp URL连接到Intellij中的数据源。我现在可以浏览表格内容了。然后,我必须将数据源分配给Intellij持久性视图中的entityManagerFactory。此外,我需要使用与application.yml中相同的NamingStrategie。

spring hibernate jpa intellij-idea jhipster
1个回答
2
投票

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

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.