简体   繁体   English

Persistence.xml 未被 Helidon MP 处理

[英]Persistence.xml not processed by Helidon MP

For my Helidon MP application I want to use H2 database with Hibernate, so I made the following configuration:对于我的 Helidon MP 应用程序,我想将 H2 数据库与 Hibernate 一起使用,因此我进行了以下配置:

    <persistence-unit name="h2" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
            <property name="hibernate.connection.driver_class" value="org.h2.Driver" />
            <property name="hibernate.connection.url" value="jdbc:h2:h2" />
            <property name="hibernate.connection.user" value="sa" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>

but my helidon application does not connect to the database with the following parameters.但是我的 helidon 应用程序没有连接到具有以下参数的数据库。 As far as I see, It simply ignores this config.据我所知,它只是忽略了这个配置。 Although I have added the following dependencies:虽然我添加了以下依赖项:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-eclipselink</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-jta-weld</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-datasource-hikaricp</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-jpa</artifactId>
    <scope>runtime</scope>
</dependency>

How can I connect to h2 database with Helidon?如何使用 Helidon 连接到 h2 数据库?

Helidon provides container-managed JPA integration, which means among many, many other things you don't specify JDBC information in your META-INF/persistence.xml file. Helidon 提供容器管理的 JPA 集成,这意味着您在META-INF/persistence.xml文件中没有指定 JDBC 信息。 The entire point of container-managed JPA is that all of this stuff is taken care of for you, so your persistence.xml classpath resource should mention a JTA-enabled data source name that should be used to connect to the database.容器管理的 JPA 的全部要点是所有这些东西都为您处理,因此您的persistence.xml类路径资源应该提到一个启用 JTA 的数据源名称,该名称应该用于连接到数据库。

Please have a look at this example: https://github.com/helidon-io/helidon/tree/helidon-3.x/examples/integrations/cdi/jpa请看这个例子: https://github.com/helidon-io/helidon/tree/helidon-3.x/examples/integrations/cdi/jpa

  1. You specify the DataSource properties in microprofile-config.properties file:您在microprofile-config.properties文件中指定DataSource属性:
javax.sql.DataSource.ds1.dataSourceClassName=org.h2.jdbcx.JdbcDataSource
javax.sql.DataSource.ds1.dataSource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
javax.sql.DataSource.ds1.dataSource.user=db_user
javax.sql.DataSource.ds1.dataSource.password=user_password
  1. Your persistence.xml should look like this:你的persistence.xml应该是这样的:
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
             version="3.0">

    <persistence-unit name="pu1" transaction-type="JTA">
        <jta-data-source>ds1</jta-data-source>
        <class>com.example.myproject.Pokemon</class>
        <class>com.example.myproject.PokemonType</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="jakarta.persistence.sql-load-script-source" value="META-INF/init_script.sql"/>
            <property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>
        </properties>
    </persistence-unit>
</persistence>
  1. Inject the EntityManager :注入EntityManager
    @PersistenceContext(unitName = "pu1")
    private EntityManager entityManager;

I highly encourage you to use Helidon Starter to generate initial projects for you: https://helidon.io/starter/3.0.2?flavor=mp&step=5&app-type=database我强烈建议您使用Helidon Starter为您生成初始项目: https://helidon.io/starter/3.0.2?flavor=mp&step=5&app-type=database

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM