Skip to content

Commit

Permalink
Added 021-quarkus-hibernate module for QUARKUS-749
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Mar 9, 2021
1 parent 14dde25 commit 49c06a0
Show file tree
Hide file tree
Showing 13 changed files with 315 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 021-quarkus-hibernate/README.md
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
```
67 changes: 67 additions & 0 deletions 021-quarkus-hibernate/pom.xml
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>
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<>();

}
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 021-quarkus-hibernate/src/main/java/io/quarkus/qe/items/Item.java
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;

}
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 021-quarkus-hibernate/src/main/java/io/quarkus/qe/items/Role.java
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;

}
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
12 changes: 12 additions & 0 deletions 021-quarkus-hibernate/src/main/resources/import.sql
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;
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"));
}
}
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 {
}
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);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<module>018-quarkus-runtime-properties</module>
<module>019-quarkus-consul</module>
<module>020-quarkus-http-non-application-endpoints</module>
<module>021-quarkus-hibernate</module>
<module>101-javaee-like-getting-started</module>
<module>201-large-static-content</module>
<module>300-quarkus-vertx-webClient</module>
Expand Down

0 comments on commit 49c06a0

Please sign in to comment.