對于我的 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 資料庫?
uj5u.com熱心網友回復:
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
- 您在檔案中指定
DataSource屬性microprofile-config.properties:
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
- 你
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>
- 注入
EntityManager:
@PersistenceContext(unitName = "pu1")
private EntityManager entityManager;
我強烈建議您使用它Helidon Starter來為您生成初始專案:https ://helidon.io/starter/3.0.2?flavor=mp&step=5&app-type=database
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/537612.html
標籤:冬眠jpa坚持直升机
