簡體   English   中英

Persistence.xml 未被 Helidon MP 處理

[英]Persistence.xml not processed by Helidon MP

對於我的 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>

但是我的 helidon 應用程序沒有連接到具有以下參數的數據庫。 據我所知,它只是忽略了這個配置。 雖然我添加了以下依賴項:

<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>

如何使用 Helidon 連接到 h2 數據庫?

Helidon 提供容器管理的 JPA 集成,這意味着您在META-INF/persistence.xml文件中沒有指定 JDBC 信息。 容器管理的 JPA 的全部要點是所有這些東西都為您處理,因此您的persistence.xml類路徑資源應該提到一個啟用 JTA 的數據源名稱,該名稱應該用於連接到數據庫。

請看這個例子: https://github.com/helidon-io/helidon/tree/helidon-3.x/examples/integrations/cdi/jpa

  1. 您在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. 你的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. 注入EntityManager
    @PersistenceContext(unitName = "pu1")
    private EntityManager entityManager;

我強烈建議您使用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