From 0308fdc7a29a3fae18cddcaba25e8c93c2e0565a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Mathieu?= Date: Fri, 10 Dec 2021 19:02:38 +0100 Subject: [PATCH] Hibernate ORM with Panache in Kotlin codestart --- .../base/README.tpl.qute.md | 4 ++ .../main/kotlin/org/acme/MyKotlinEntity.kt | 31 --------- .../org/acme/MyKotlinEntity.tpl.qute.kt | 67 +++++++++++++++++++ .../resources/META-INF/quarkus-extension.yaml | 4 ++ .../quarkus/HibernateOrmCodestartTest.java | 1 - ...ibernateOrmPanacheKotlinCodestartTest.java | 30 +++++++++ .../quarkus/QuarkusCodestartBuildIT.java | 37 +++++----- ...in_kotlin_ilove_quark_us_MyKotlinEntity.kt | 2 +- ...src_main_java_ilove_quark_us_MyEntity.java | 47 +++++-------- ...in_kotlin_ilove_quark_us_MyKotlinEntity.kt | 34 ++++++++++ 10 files changed, 174 insertions(+), 83 deletions(-) delete mode 100644 devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/kotlin/src/main/kotlin/org/acme/MyKotlinEntity.kt create mode 100644 devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/kotlin/src/main/kotlin/org/acme/MyKotlinEntity.tpl.qute.kt create mode 100644 integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmPanacheKotlinCodestartTest.java create mode 100644 integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheKotlinCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt diff --git a/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/base/README.tpl.qute.md b/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/base/README.tpl.qute.md index f44adc1d3b94dd..312518fd917889 100644 --- a/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/base/README.tpl.qute.md +++ b/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/base/README.tpl.qute.md @@ -2,4 +2,8 @@ {#if input.selected-extensions-ga.contains('io.quarkus:quarkus-hibernate-orm-panache')} [Related Hibernate with Panache section...](https://quarkus.io/guides/hibernate-orm-panache) +{/if} + +{#if input.selected-extensions-ga.contains('io.quarkus:quarkus-hibernate-orm-panache-kotlin')} +[Related Hibernate with Panache in Kotlin section...](https://quarkus.io/guides/hibernate-orm-panache-kotlin) {/if} \ No newline at end of file diff --git a/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/kotlin/src/main/kotlin/org/acme/MyKotlinEntity.kt b/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/kotlin/src/main/kotlin/org/acme/MyKotlinEntity.kt deleted file mode 100644 index e37809020e2cf9..00000000000000 --- a/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/kotlin/src/main/kotlin/org/acme/MyKotlinEntity.kt +++ /dev/null @@ -1,31 +0,0 @@ -package org.acme - -import javax.persistence.Entity -import javax.persistence.GeneratedValue -import javax.persistence.Id - -/** - * Example JPA entity. - * - * To use it, get access to a JPA EntityManager via injection. - * - * {@code - * @Inject - * lateinit var em:EntityManager; - * - * fun doSomething() { - * val entity1 = MyKotlinEntity(); - * entity1.field = "field-1" - * em.persist(entity1); - * - * val entities:List = em.createQuery("from MyEntity", MyKotlinEntity::class.java).getResultList() - * } - * } - */ -@Entity -class MyKotlinEntity { - @get:GeneratedValue - @get:Id - var id: Long? = null - var field: String? = null -} \ No newline at end of file diff --git a/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/kotlin/src/main/kotlin/org/acme/MyKotlinEntity.tpl.qute.kt b/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/kotlin/src/main/kotlin/org/acme/MyKotlinEntity.tpl.qute.kt new file mode 100644 index 00000000000000..71793b36ebafbf --- /dev/null +++ b/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/kotlin/src/main/kotlin/org/acme/MyKotlinEntity.tpl.qute.kt @@ -0,0 +1,67 @@ +package org.acme + +{#if !input.selected-extensions-ga.contains('io.quarkus:quarkus-hibernate-orm-panache-kotlin')} +import javax.persistence.Entity +import javax.persistence.GeneratedValue +import javax.persistence.Id + +/** + * Example JPA entity. + * + * To use it, get access to a JPA EntityManager via injection. + * + * \{@code + * @Inject + * lateinit var em:EntityManager; + * + * fun doSomething() { + * val entity1 = MyKotlinEntity(); + * entity1.field = "field-1" + * em.persist(entity1); + * + * val entities:List = em.createQuery("from MyEntity", MyKotlinEntity::class.java).getResultList() + * } + * } + */ +@Entity +class MyKotlinEntity { + @get:GeneratedValue + @get:Id + var id: Long? = null + var field: String? = null +} +{/if} +{#if input.selected-extensions-ga.contains('io.quarkus:quarkus-hibernate-orm-panache-kotlin')} +import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity +import io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanion +import javax.persistence.Entity + +/** + * Example JPA entity defined as a Kotlin Panache Entity. + * An ID field of Long type is provided, if you want to define your own ID field extends PanacheEntityBase instead. + * + * This uses the active record pattern, you can also use the repository pattern instead: + * {@see https://quarkus.io/guides/hibernate-orm-panache-kotlin#defining-your-repository}. + * + * Usage (more example on the documentation) + * + * \{@code + * + * fun doSomething() { + * val entity1 = MyKotlinEntity(); + * entity1.field = "field-1" + * entity1.persist() + * + * val entities:List = MyKotlinEntity.listAll() + * } + * } + */ +@Entity +class MyKotlinEntity: PanacheEntity() { + companion object: PanacheCompanion { + fun byName(name: String) = list("name", name) + } + + lateinit var field: String +} +{/if} \ No newline at end of file diff --git a/extensions/panache/hibernate-orm-panache-kotlin/runtime/src/main/resources/META-INF/quarkus-extension.yaml b/extensions/panache/hibernate-orm-panache-kotlin/runtime/src/main/resources/META-INF/quarkus-extension.yaml index f00c0f1c3f7844..34d647e63cedfb 100644 --- a/extensions/panache/hibernate-orm-panache-kotlin/runtime/src/main/resources/META-INF/quarkus-extension.yaml +++ b/extensions/panache/hibernate-orm-panache-kotlin/runtime/src/main/resources/META-INF/quarkus-extension.yaml @@ -15,3 +15,7 @@ metadata: config: - "quarkus.datasource." - "quarkus.hibernate-orm." + codestart: + name: "hibernate-orm" + languages: [ "kotlin" ] + artifact: "io.quarkus:quarkus-project-core-extension-codestarts" diff --git a/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmCodestartTest.java b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmCodestartTest.java index c1df9ba9f488a0..179645bda9eb03 100644 --- a/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmCodestartTest.java +++ b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmCodestartTest.java @@ -1,7 +1,6 @@ package io.quarkus.devtools.codestarts.quarkus; import static io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog.Language.JAVA; -import static io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog.Language.KOTLIN; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; diff --git a/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmPanacheKotlinCodestartTest.java b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmPanacheKotlinCodestartTest.java new file mode 100644 index 00000000000000..7fe535c799a6e0 --- /dev/null +++ b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmPanacheKotlinCodestartTest.java @@ -0,0 +1,30 @@ +package io.quarkus.devtools.codestarts.quarkus; + +import static io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog.Language.KOTLIN; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.devtools.testing.codestarts.QuarkusCodestartTest; +import io.quarkus.maven.ArtifactKey; + +public class HibernateOrmPanacheKotlinCodestartTest { + + @RegisterExtension + public static QuarkusCodestartTest codestartTest = QuarkusCodestartTest.builder() + .codestarts("hibernate-orm") + .extension(new ArtifactKey("io.quarkus", "quarkus-jdbc-h2")) + .extension(new ArtifactKey("io.quarkus", "quarkus-hibernate-orm-panache-kotlin")) + .languages(KOTLIN) + .build(); + + @Test + void testContent() throws Throwable { + codestartTest.checkGeneratedSource("org.acme.MyKotlinEntity"); + } + + @Test + void buildAllProjectsForLocalUse() throws Throwable { + codestartTest.buildAllProjects(); + } +} diff --git a/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/QuarkusCodestartBuildIT.java b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/QuarkusCodestartBuildIT.java index 86356a65e0b387..415070a21d879a 100644 --- a/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/QuarkusCodestartBuildIT.java +++ b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/QuarkusCodestartBuildIT.java @@ -1,19 +1,7 @@ package io.quarkus.devtools.codestarts.quarkus; -import com.google.common.collect.Sets; -import io.quarkus.devtools.codestarts.Codestart; -import io.quarkus.devtools.codestarts.CodestartProjectDefinition; -import io.quarkus.devtools.project.BuildTool; -import io.quarkus.devtools.testing.PlatformAwareTestBase; -import io.quarkus.devtools.testing.SnapshotTesting; -import io.quarkus.devtools.testing.WrapperRunner; -import io.quarkus.devtools.testing.codestarts.QuarkusCodestartTesting; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; import java.nio.file.Path; @@ -26,8 +14,22 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static java.util.Collections.singletonList; -import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import com.google.common.collect.Sets; + +import io.quarkus.devtools.codestarts.Codestart; +import io.quarkus.devtools.codestarts.CodestartProjectDefinition; +import io.quarkus.devtools.project.BuildTool; +import io.quarkus.devtools.testing.PlatformAwareTestBase; +import io.quarkus.devtools.testing.SnapshotTesting; +import io.quarkus.devtools.testing.WrapperRunner; +import io.quarkus.devtools.testing.codestarts.QuarkusCodestartTesting; @TestInstance(TestInstance.Lifecycle.PER_CLASS) class QuarkusCodestartBuildIT extends PlatformAwareTestBase { @@ -38,8 +40,7 @@ class QuarkusCodestartBuildIT extends PlatformAwareTestBase { "spring-web-codestart", "picocli-codestart", "hibernate-orm-codestart", - "reactive-messaging-codestart", - "hibernate-orm-panache-codestart"); + "reactive-messaging-codestart"); @BeforeAll static void setUp() throws IOException { diff --git a/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt index 9f9ca0a6092cf6..ae6ca99d523eb5 100644 --- a/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt +++ b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt @@ -28,4 +28,4 @@ class MyKotlinEntity { @get:Id var id: Long? = null var field: String? = null -} \ No newline at end of file +} diff --git a/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java index 5e512e9cbdf02d..72205306cfc554 100644 --- a/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java +++ b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java @@ -1,47 +1,30 @@ package ilove.quark.us; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; + import io.quarkus.hibernate.orm.panache.PanacheEntity; + + import javax.persistence.Entity; + /** - * Example JPA entity. + * Example JPA entity defined as a Panache Entity. + * An ID field of Long type is provided, if you want to define your own ID field extends PanacheEntityBase instead. * - * To use it, get access to a JPA EntityManager via injection. + * This uses the active record pattern, you can also use the repository pattern instead: + * . * - * {@code - * @Inject - * EntityManager em; + * Usage (more example on the documentation) * + * {@code * public void doSomething() { * MyEntity entity1 = new MyEntity(); - * entity1.setField("field-1"); - * em.persist(entity1); + * entity1.field = "field-1"; + * entity1.persist(); * - * List entities = em.createQuery("from MyEntity", MyEntity.class).getResultList(); + * List entities = MyEntity.listAll(); * } * } */ @Entity -public class MyEntity { - private Long id; - private String field; - - @Id - @GeneratedValue - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getField() { - return field; - } - - public void setField(String field) { - this.field = field; - } +public class MyEntity extends PanacheEntity { + public String field; } diff --git a/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheKotlinCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheKotlinCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt new file mode 100644 index 00000000000000..0c21af1ce75f3f --- /dev/null +++ b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheKotlinCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt @@ -0,0 +1,34 @@ +package ilove.quark.us + +import io.quarkus.hibernate.orm.panache.kotlin.PanacheEntity +import io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanion +import javax.persistence.Entity + +/** + * Example JPA entity defined as a Kotlin Panache Entity. + * An ID field of Long type is provided, if you want to define your own ID field extends PanacheEntityBase instead. + * + * This uses the active record pattern, you can also use the repository pattern instead: + * . + * + * Usage (more example on the documentation) + * + * {@code + * + * fun doSomething() { + * val entity1 = MyKotlinEntity(); + * entity1.field = "field-1" + * entity1.persist() + * + * val entities:List = MyKotlinEntity.listAll() + * } + * } + */ +@Entity +class MyKotlinEntity: PanacheEntity() { + companion object: PanacheCompanion { + fun byName(name: String) = list("name", name) + } + + lateinit var field: String +}