对于我的Helidon应用程序,我想在Hibernate中使用H2数据库,所以我做了以下配置:
<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数据库?
发布于 2022-11-17 13:56:52
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
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_passwordpersistence.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://stackoverflow.com/questions/74476458
复制相似问题