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 405280158877d..312518fd91788 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 @@ -1 +1,9 @@ -{#include readme-header /} \ No newline at end of file +{#include readme-header /} + +{#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/java/src/main/java/org/acme/MyEntity.java b/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/java/src/main/java/org/acme/MyEntity.java deleted file mode 100644 index c28a608e6020e..0000000000000 --- a/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/java/src/main/java/org/acme/MyEntity.java +++ /dev/null @@ -1,47 +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 - * EntityManager em; - * - * public void doSomething() { - * MyEntity entity1 = new MyEntity(); - * entity1.setField("field-1"); - * em.persist(entity1); - * - * List entities = em.createQuery("from MyEntity", MyEntity.class).getResultList(); - * } - * } - */ -@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; - } -} diff --git a/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/java/src/main/java/org/acme/MyEntity.tpl.qute.java b/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/java/src/main/java/org/acme/MyEntity.tpl.qute.java new file mode 100644 index 0000000000000..ae11050e3e1c8 --- /dev/null +++ b/devtools/project-core-extension-codestarts/src/main/resources/codestarts/quarkus/extension-codestarts/hibernate-orm-codestart/java/src/main/java/org/acme/MyEntity.tpl.qute.java @@ -0,0 +1,63 @@ +package org.acme; + +{#if !input.selected-extensions-ga.contains('io.quarkus:quarkus-hibernate-orm-panache')} +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 + * EntityManager em; + * + * public void doSomething() { + * MyEntity entity1 = new MyEntity(); + * entity1.field = "field-1"; + * em.persist(entity1); + * + * List entities = em.createQuery("from MyEntity", MyEntity.class).getResultList(); + * } + * } + */ +@Entity +public class MyEntity { + @Id + @GeneratedValue + public Long id; + + public String field; +} +{/if} +{#if input.selected-extensions-ga.contains('io.quarkus:quarkus-hibernate-orm-panache')} +import io.quarkus.hibernate.orm.panache.PanacheEntity; +import javax.persistence.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. + * + * This uses the active record pattern, you can also use the repository pattern instead: + * {@see https://quarkus.io/guides/hibernate-orm-panache#solution-2-using-the-repository-pattern}. + * + * Usage (more example on the documentation) + * + * \{@code + * public void doSomething() { + * MyEntity entity1 = new MyEntity(); + * entity1.field = "field-1"; + * entity1.persist(); + * + * List entities = MyEntity.listAll(); + * } + * } + */ +@Entity +public class MyEntity extends PanacheEntity { + public String field; +} +{/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.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 0000000000000..71793b36ebafb --- /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/docs/src/main/asciidoc/hibernate-orm-panache-kotlin.adoc b/docs/src/main/asciidoc/hibernate-orm-panache-kotlin.adoc index d1f109e6f0e62..1e5cd6e717092 100644 --- a/docs/src/main/asciidoc/hibernate-orm-panache-kotlin.adoc +++ b/docs/src/main/asciidoc/hibernate-orm-panache-kotlin.adoc @@ -25,7 +25,7 @@ change or two. To Panache-enable your entity, you would define it something lik [source,kotlin] ---- @Entity -class Person: PanacheEntity { +class Person: PanacheEntity() { lateinit var name: String lateinit var birth: LocalDate lateinit var status: Status 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 c75691278074f..9b9bdc7c48871 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/extensions/panache/hibernate-orm-panache/runtime/src/main/resources/META-INF/quarkus-extension.yaml b/extensions/panache/hibernate-orm-panache/runtime/src/main/resources/META-INF/quarkus-extension.yaml index 5894719a99f9c..80b0d0900f65a 100644 --- a/extensions/panache/hibernate-orm-panache/runtime/src/main/resources/META-INF/quarkus-extension.yaml +++ b/extensions/panache/hibernate-orm-panache/runtime/src/main/resources/META-INF/quarkus-extension.yaml @@ -14,3 +14,7 @@ metadata: config: - "quarkus.datasource." - "quarkus.hibernate-orm." + codestart: + name: "hibernate-orm" + languages: ["java"] + artifact: "io.quarkus:quarkus-project-core-extension-codestarts" diff --git a/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmPanacheCodestartTest.java b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmPanacheCodestartTest.java new file mode 100644 index 0000000000000..0c9c49c2e2d1e --- /dev/null +++ b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/quarkus/HibernateOrmPanacheCodestartTest.java @@ -0,0 +1,30 @@ +package io.quarkus.devtools.codestarts.quarkus; + +import static io.quarkus.devtools.codestarts.quarkus.QuarkusCodestartCatalog.Language.JAVA; + +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 HibernateOrmPanacheCodestartTest { + + @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")) + .languages(JAVA) + .build(); + + @Test + void testContent() throws Throwable { + codestartTest.checkGeneratedSource("org.acme.MyEntity"); + } + + @Test + void buildAllProjectsForLocalUse() throws Throwable { + codestartTest.buildAllProjects(); + } +} 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 0000000000000..7fe535c799a6e --- /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/resources/__snapshots__/HibernateOrmCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java index 5e512e9cbdf02..09329e1879b2b 100644 --- a/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java +++ b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java @@ -15,7 +15,7 @@ * * public void doSomething() { * MyEntity entity1 = new MyEntity(); - * entity1.setField("field-1"); + * entity1.field = "field-1"; * em.persist(entity1); * * List entities = em.createQuery("from MyEntity", MyEntity.class).getResultList(); @@ -24,24 +24,9 @@ */ @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 Long id; - public void setField(String field) { - this.field = field; - } + public String field; } 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 new file mode 100644 index 0000000000000..7af15a045441e --- /dev/null +++ b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheCodestartTest/testContent/src_main_java_ilove_quark_us_MyEntity.java @@ -0,0 +1,29 @@ +package ilove.quark.us; + +import io.quarkus.hibernate.orm.panache.PanacheEntity; +import javax.persistence.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. + * + * This uses the active record pattern, you can also use the repository pattern instead: + * . + * + * Usage (more example on the documentation) + * + * {@code + * public void doSomething() { + * MyEntity entity1 = new MyEntity(); + * entity1.field = "field-1"; + * entity1.persist(); + * + * List entities = MyEntity.listAll(); + * } + * } + */ +@Entity +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 0000000000000..1bacaf93ee59b --- /dev/null +++ b/integration-tests/devtools/src/test/resources/__snapshots__/HibernateOrmPanacheKotlinCodestartTest/testContent/src_main_kotlin_ilove_quark_us_MyKotlinEntity.kt @@ -0,0 +1,31 @@ +package ilove.quark.us + +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. + * + * ```kotlin + * @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