Skip to content

Commit

Permalink
Add an Hibernate ORM codestart
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Oct 21, 2021
1 parent 201f3bc commit a4e0a29
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{#include readme-header /}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
quarkus:
hibernate-orm:
database:
generation: drop-and-create
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- This file allow to write SQL commands that will be emitted in test and dev.
-- The commands are commented as their support depends of the database
-- insert into myentity (id, field) values(nextval('hibernate_sequence'), 'field-1');
-- insert into myentity (id, field) values(nextval('hibernate_sequence'), 'field-2');
-- insert into myentity (id, field) values(nextval('hibernate_sequence'), 'field-3');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: hibernate-orm-codestart
ref: hibernate-orm
tags: extension-codestart
type: code
metadata:
title: Hibernate ORM
description: Create your first JPA entity
related-guide-section: https://quarkus.io/guides/hibernate-orm
language:
base:
dependencies:
- io.quarkus:quarkus-hibernate-orm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.acme;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.acme;

import io.quarkus.test.TestTransaction;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.util.List;

@QuarkusTest
public class MyEntityTest {
@Inject
EntityManager em;

@Test
@TestTransaction
public void testMyEntity() {
// create 3 entities, this can be replaces by inserts inside the generated src/main/resources/import.sql file
MyEntity entity1 = new MyEntity();
entity1.setField("field-1");
em.persist(entity1);
MyEntity entity2 = new MyEntity();
entity2.setField("field-2");
em.persist(entity2);
MyEntity entity3 = new MyEntity();
entity1.setField("field-3");
em.persist(entity3);

// list the 3 created entities
List<MyEntity> entities = em.createQuery("from MyEntity", MyEntity.class).getResultList();
Assertions.assertEquals(3, entities.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
@@ -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.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.devtools.testing.codestarts.QuarkusCodestartTest;

public class HibernateOrmCodestartTest {

@RegisterExtension
public static QuarkusCodestartTest codestartTest = QuarkusCodestartTest.builder()
.codestarts("hibernate-orm")
.languages(JAVA)
.build();

@Test
void testContent() throws Throwable {
codestartTest.checkGeneratedSource("org.acme.MyEntity");
codestartTest.checkGeneratedTestSource("org.acme.MyEntityTest");
}

@Test
@EnabledIfSystemProperty(named = "build-projects", matches = "true")
void buildAllProjectsForLocalUse() throws Throwable {
codestartTest.buildAllProjects();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ilove.quark.us;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ilove.quark.us;

import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.util.List;

@QuarkusTest
public class MyEntityTest {
@Inject
EntityManager em;

@Test
public void testMyEntity() {
List<MyEntity> entities = em.createQuery("from MyEntity", MyEntity.class).getResultList();
Assertions.assertEquals(3, entities.size());
}

}

0 comments on commit a4e0a29

Please sign in to comment.