Skip to content

Commit

Permalink
don’t depend on Micronaut Data JPA
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo committed Nov 11, 2023
1 parent eca0288 commit 2e95a6c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.guides.feature;

import io.micronaut.context.annotation.Replaces;
import io.micronaut.starter.application.ApplicationType;
import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.build.dependencies.MicronautDependencyUtils;
import io.micronaut.starter.feature.Category;
import io.micronaut.starter.feature.FeatureContext;
import io.micronaut.starter.feature.database.jdbc.JdbcFeature;
import io.micronaut.starter.feature.migration.MigrationFeature;
import io.micronaut.starter.feature.database.*;
import jakarta.inject.Singleton;

@Replaces(HibernateJpa.class)
@Singleton
public class HibernateJpaReplacement extends HibernateJpa {

private static final String ARTIFACT_ID_MICRONAUT_DATA_TX_HIBERNATE = "micronaut-data-tx-hibernate";
private static final String ARTIFACT_ID_MICRONAUT_HIBERNATE_JPA = "micronaut-hibernate-jpa";
private static final String ARTIFACT_ID_MICRONAUT_DATA_MODEL = "micronaut-data-model";

public HibernateJpaReplacement(JdbcFeature jdbcFeature) {
super(jdbcFeature);
}

@Override
public void apply(GeneratorContext generatorContext) {
generatorContext.getConfiguration().put(JPA_HIBERNATE_PROPERTIES_HBM2DDL,
generatorContext.getFeatures().hasFeature(MigrationFeature.class) ? Hbm2ddlAuto.NONE.toString() :
Hbm2ddlAuto.UPDATE.toString());
addDependencies(generatorContext);
}

protected void addDependencies(GeneratorContext generatorContext) {
generatorContext.addDependency(MicronautDependencyUtils.sqlDependency()
.artifactId(ARTIFACT_ID_MICRONAUT_HIBERNATE_JPA)
.compile());
generatorContext.addDependency(MicronautDependencyUtils.dataDependency()
.artifactId(ARTIFACT_ID_MICRONAUT_DATA_TX_HIBERNATE)
.compile());
generatorContext.addDependency(MicronautDependencyUtils.dataDependency()
.artifactId(ARTIFACT_ID_MICRONAUT_DATA_MODEL)
.compile());
}
}
5 changes: 3 additions & 2 deletions guides/micronaut-jpa-hibernate/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
"title": "Access a database with JPA and Hibernate",
"intro": "Learn how to access a database with JPA and Hibernate using the Micronaut framework.",
"authors": ["Iván López", "Sergio del Amo"],
"tags": ["database", "jpa", "hibernate", "h2"],
"tags": [],
"categories": ["Data Access"],
"publicationDate": "2018-09-01",
"languages": ["java", "groovy"],
"skipMavenTests": true,
"apps": [
{
"name": "default",
"features": ["graalvm", "hibernate-jpa", "data-jpa", "serialization-jackson", "validation"]
"features": ["graalvm", "h2", "hibernate-jpa", "serialization-jackson", "validation"]
}
]
}
30 changes: 15 additions & 15 deletions guides/micronaut-jpa-hibernate/micronaut-jpa-hibernate.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ Add the following dependencies:
:dependencies:

dependency:micronaut-hibernate-jpa[groupId=io.micronaut.sql,callout=1]
dependency:micronaut-jdbc-hikari[groupId=io.micronaut.sql,callout=2]
dependency:h2[groupId=com.h2database,scope=runtimeOnly,callout=3]
dependency:jakarta.persistence-api[groupId=jakarta.persistence,version=2.2.3,callout=4]
dependency:micronaut-data-tx-hibernate[groupId=io.micronaut.data,callout=2]
dependency:micronaut-jdbc-hikari[groupId=io.micronaut.sql,callout=3]
dependency:h2[groupId=com.h2database,scope=runtimeOnly,callout=4]
dependency:micronaut-data-model[groupId=io.micronaut.data]

:dependencies:

<1> Configures Hibernate/JPA EntityManagerFactory beans.
<2> Configures SQL DataSource instances using Hikari Connection Pool.
<3> Add dependency to in-memory H2 Database.
<4> Add dependency to Jakarta Persistence API.
<2> Adds Micronaut Data Transaction Hibernate dependency.
<3> Configures SQL DataSource instances using Hikari Connection Pool.
<4> Add dependency to in-memory H2 Database.
<5> Add dependency to Jakarta Persistence API.

=== Data Source configuration

Expand Down Expand Up @@ -69,19 +71,19 @@ You can override `max` if you add to your `src/main/resources/application.proper

resource:application.properties[tag=application]

=== Repository Access

To mark the transaction demarcations, we use the https://jakarta.ee/specifications/transactions/2.0/apidocs/jakarta/transaction/transactional[Jakarta jakarta.transaction.Transactional annotation].
=== Validation

To use it, you have to include the `micronaut-data-processor` dependency in your annotation processor configuration:
common:validation.adoc[]

dependency:micronaut-data-hibernate-jpa[groupId=io.micronaut.data,scope=annotationProcessor]
=== Repository Access

Next, create a repository interface to define the operations to access the database:
Next, create a repository interface to define the operations to access the database.

source:GenreRepository[]

The implementation:
common:transaction-demarcations.adoc[]

Write the implementation:

source:GenreRepositoryImpl[]

Expand All @@ -91,8 +93,6 @@ callout:transactional[3]

=== Controller

common:validation.adoc[]

Create two classes to encapsulate Save and Update operations:

source:GenreSaveCommand[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
To mark the transaction demarcations, we use the https://jakarta.ee/specifications/transactions/2.0/apidocs/jakarta/transaction/transactional[Jakarta jakarta.transaction.Transactional annotation].
14 changes: 11 additions & 3 deletions src/docs/common/snippets/common-validation.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Micronaut validation is built on the standard framework – https://www.jcp.org/en/jsr/detail?id=380[JSR 380], also known as Bean Validation 2.0.
https://micronaut-projects.github.io/micronaut-validation/snapshot/guide/[Micronaut validation] is built on the standard framework – https://www.jcp.org/en/jsr/detail?id=380[JSR 380], also known as Bean Validation 2.0. Micronaut Validation has built-in support for validation of beans that are annotated with `jakarta.validation` annotations.

Hibernate Validator is a reference implementation of the validation API. Micronaut https://docs.micronaut.io/latest/guide/#beanValidation[has built-in support for validation of beans] that are annotated with `javax.validation` annotations.
To use Micronaut Validation, you need the following dependencies:

:dependencies:

dependency:micronaut-validation-processor[groupId=io.micronaut.validation,scope=annotationProcessor]
dependency:micronaut-validation[groupId=io.micronaut.validation]

:dependencies:

Alternatively, you can use https://micronaut-projects.github.io/micronaut-hibernate-validator/latest/guide/[Micronaut Hibernate Validator], which uses https://hibernate.org/validator/[Hibernate Validator]; a reference implementation of the validation API.

The necessary dependencies are included by default when creating a new application, so you don't need to add anything else.

0 comments on commit 2e95a6c

Please sign in to comment.