diff --git a/buildSrc/src/main/java/io/micronaut/guides/feature/HibernateJpaReplacement.java b/buildSrc/src/main/java/io/micronaut/guides/feature/HibernateJpaReplacement.java new file mode 100644 index 0000000000..b83bd5be10 --- /dev/null +++ b/buildSrc/src/main/java/io/micronaut/guides/feature/HibernateJpaReplacement.java @@ -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()); + } +} diff --git a/guides/micronaut-jpa-hibernate/metadata.json b/guides/micronaut-jpa-hibernate/metadata.json index cb62a7907a..1ef9d9a289 100644 --- a/guides/micronaut-jpa-hibernate/metadata.json +++ b/guides/micronaut-jpa-hibernate/metadata.json @@ -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"] } ] } diff --git a/guides/micronaut-jpa-hibernate/micronaut-jpa-hibernate.adoc b/guides/micronaut-jpa-hibernate/micronaut-jpa-hibernate.adoc index fdc4a5004e..6b73d3a219 100644 --- a/guides/micronaut-jpa-hibernate/micronaut-jpa-hibernate.adoc +++ b/guides/micronaut-jpa-hibernate/micronaut-jpa-hibernate.adoc @@ -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 @@ -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[] @@ -91,8 +93,6 @@ callout:transactional[3] === Controller -common:validation.adoc[] - Create two classes to encapsulate Save and Update operations: source:GenreSaveCommand[] diff --git a/src/docs/common/snippets/common-transaction-demarcations.adoc b/src/docs/common/snippets/common-transaction-demarcations.adoc new file mode 100644 index 0000000000..e947e055dc --- /dev/null +++ b/src/docs/common/snippets/common-transaction-demarcations.adoc @@ -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]. \ No newline at end of file diff --git a/src/docs/common/snippets/common-validation.adoc b/src/docs/common/snippets/common-validation.adoc index b5664e62b5..926f20741a 100644 --- a/src/docs/common/snippets/common-validation.adoc +++ b/src/docs/common/snippets/common-validation.adoc @@ -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.