For my Helidon MP application I want to use H2 database with Hibernate, so I made the following configuration:
<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. 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?
CodePudding user response:
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. 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.
Please have a look at this example: https://github.com/helidon-io/helidon/tree/helidon-3.x/examples/integrations/cdi/jpa
- You specify the
DataSource
properties inmicroprofile-config.properties
file:
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
- Your
persistence.xml
should look like this:
<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>
- Inject the
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