-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 021-quarkus-hibernate module for QUARKUS-749
- Loading branch information
Showing
13 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Module that covers intergation with Hibernate features: | ||
- Usage of Code Generator: `hibernate-jpamodelgen` dependency | ||
- Reproducer for https://issues.redhat.com/browse/QUARKUS-749: possible data loss bug in hibernate | ||
|
||
In order to run this module, you need to start up a Postgres instance: | ||
|
||
``` | ||
docker run -it -p 5432:5432 -e POSTGRES_PASSWORD=connor -e POSTGRES_USER=sarah -e POSTGRES_DB=test postgres:13.1 | ||
``` | ||
|
||
And then: | ||
|
||
``` | ||
mvn compile quarkus:dev | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version="1.0"?> | ||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.quarkus.qe</groupId> | ||
<artifactId>beefy-scenarios</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>021-quarkus-hibernate</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-hibernate-orm</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-jdbc-postgresql</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-hibernate-validator</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.hibernate</groupId> | ||
<artifactId>hibernate-jpamodelgen</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>generate-code</goal> | ||
<goal>generate-code-tests</goal> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
37 changes: 37 additions & 0 deletions
37
021-quarkus-hibernate/src/main/java/io/quarkus/qe/items/Account.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.quarkus.qe.items; | ||
|
||
import java.util.Date; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.JoinTable; | ||
import javax.persistence.ManyToMany; | ||
import javax.persistence.Temporal; | ||
import javax.persistence.TemporalType; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
|
||
@Entity | ||
public class Account { | ||
|
||
@Id | ||
public Long id; | ||
|
||
@Column(length = 255, unique = true, nullable = false) | ||
@NotNull | ||
@Size(max = 255) | ||
public String email; | ||
|
||
@Temporal(TemporalType.TIMESTAMP) | ||
public Date createdOn; | ||
|
||
@ManyToMany(fetch = FetchType.LAZY) | ||
@JoinTable(name = "account_in_role", joinColumns = @JoinColumn(name = "accountid"), inverseJoinColumns = @JoinColumn(name = "roleid")) | ||
public Set<Role> roles = new HashSet<>(); | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
021-quarkus-hibernate/src/main/java/io/quarkus/qe/items/Customer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.quarkus.qe.items; | ||
|
||
import java.util.Date; | ||
|
||
import javax.persistence.CascadeType; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.Id; | ||
import javax.persistence.OneToOne; | ||
import javax.persistence.Temporal; | ||
import javax.persistence.TemporalType; | ||
import javax.persistence.Version; | ||
|
||
@Entity | ||
public class Customer { | ||
|
||
@Id | ||
public Long id; | ||
|
||
@Version | ||
@Column(name = "version") | ||
public int version; | ||
|
||
@Temporal(TemporalType.TIMESTAMP) | ||
public Date createdOn; | ||
|
||
@OneToOne(optional = false, fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}) | ||
public Account account; | ||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
021-quarkus-hibernate/src/main/java/io/quarkus/qe/items/Item.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.qe.items; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
|
||
@Entity | ||
public class Item { | ||
|
||
@Id | ||
public Long id; | ||
|
||
public String note; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "customerId") | ||
public Customer customer; | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
021-quarkus-hibernate/src/main/java/io/quarkus/qe/items/ItemsResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.quarkus.qe.items; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.TypedQuery; | ||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.Join; | ||
import javax.persistence.criteria.Root; | ||
import javax.transaction.Transactional; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
@Path("/items") | ||
@Transactional | ||
public class ItemsResource { | ||
|
||
@Inject | ||
EntityManager em; | ||
|
||
@GET | ||
@Path("/count") | ||
public int countOrders() { | ||
CriteriaBuilder cb = em.getCriteriaBuilder(); | ||
CriteriaQuery<Item> cq = cb.createQuery(Item.class); | ||
Root<Item> root = cq.from(Item.class); | ||
Join<Item, Customer> custJoin = (Join<Item, Customer>) root.fetch(Item_.customer); | ||
|
||
TypedQuery<Item> query = em.createQuery(cq); | ||
return query.getResultList().size(); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
021-quarkus-hibernate/src/main/java/io/quarkus/qe/items/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.quarkus.qe.items; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
public class Role { | ||
|
||
@Id | ||
public Long id; | ||
|
||
@Column | ||
public String name; | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
021-quarkus-hibernate/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
quarkus.datasource.db-kind=postgresql | ||
quarkus.datasource.username=sarah | ||
quarkus.datasource.password=connor | ||
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/postgres?currentSchema=test | ||
quarkus.hibernate-orm.database.default-schema=test | ||
quarkus.hibernate-orm.database.generation=drop-and-create | ||
quarkus.hibernate-orm.database.generation.create-schemas=true | ||
quarkus.hibernate-orm.sql-load-script=import.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
truncate table item cascade ; | ||
truncate table customer cascade ; | ||
truncate table account_in_role cascade ; | ||
truncate table account cascade ; | ||
truncate table role cascade ; | ||
start TRANSACTION ; | ||
insert into account (id, email) values (1, '[email protected]'); | ||
insert into role (id, name) values (1, 'admin'); | ||
insert into account_in_role (accountid, roleid) values (1, 1); | ||
insert into customer (id, version, account_id) values (1, 1, 1); | ||
insert into item (id, note, customerid) values (1, 'Item 1', 1); | ||
commit; |
23 changes: 23 additions & 0 deletions
23
021-quarkus-hibernate/src/test/java/io/quarkus/qe/ItemsResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.qe; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.qe.containers.PostgreSqlDatabaseTestResource; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
@QuarkusTestResource(PostgreSqlDatabaseTestResource.class) | ||
public class ItemsResourceTest { | ||
|
||
/** | ||
* Required data is pulled in from the `import.sql` resource. | ||
*/ | ||
@Test | ||
public void shouldNotFailWithConstraints() { | ||
given().when().get("/items/count").then().body(is("1")); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
021-quarkus-hibernate/src/test/java/io/quarkus/qe/NativeItemsResourceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.quarkus.qe; | ||
|
||
import io.quarkus.test.junit.NativeImageTest; | ||
|
||
@NativeImageTest | ||
public class NativeItemsResourceIT extends ItemsResourceTest { | ||
} |
43 changes: 43 additions & 0 deletions
43
...rkus-hibernate/src/test/java/io/quarkus/qe/containers/PostgreSqlDatabaseTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.quarkus.qe.containers; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
|
||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
|
||
public class PostgreSqlDatabaseTestResource implements QuarkusTestResourceLifecycleManager { | ||
|
||
private static final String QUARKUS_DB_KIND = "quarkus.datasource.db-kind"; | ||
private static final String QUARKUS_DB_JDBC_URL = "quarkus.datasource.jdbc.url"; | ||
private static final String QUARKUS_DB_USER = "quarkus.datasource.username"; | ||
private static final String QUARKUS_DB_PASSWORD = "quarkus.datasource.password"; | ||
private static final String DEFAULT_SCHEMA = "test"; | ||
private static final String POSTGRESQL = "postgresql"; | ||
|
||
private PostgreSQLContainer<?> container; | ||
|
||
@Override | ||
public Map<String, String> start() { | ||
container = new PostgreSQLContainer<>("postgres:13.1"); | ||
container.withUrlParam("currentSchema", DEFAULT_SCHEMA); | ||
container.start(); | ||
|
||
Map<String, String> config = new HashMap<>(); | ||
config.put(QUARKUS_DB_KIND, POSTGRESQL); | ||
config.put(QUARKUS_DB_JDBC_URL, container.getJdbcUrl()); | ||
config.put(QUARKUS_DB_USER, container.getUsername()); | ||
config.put(QUARKUS_DB_PASSWORD, container.getPassword()); | ||
|
||
return Collections.unmodifiableMap(config); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
Optional.ofNullable(container).ifPresent(GenericContainer::stop); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters