-
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.
Updated module 004 module to reproduce data loss issues
- Loading branch information
Showing
10 changed files
with
177 additions
and
2 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Quarkus - Hibernate and Hibernate Validator (lazy fetch strategy) | ||
This scenario uses in-memory Java database H2. | ||
Test class annotated with `@QuarkusTestResource(H2DatabaseTestResource.class)` starts an in-memory H2 database | ||
Test class annotated with `@QuarkusTestResource(H2DatabaseTestResource.class)` starts an in-memory H2 database | ||
|
||
Module that covers integration with some Hibernate features like: | ||
- Reproducer for [14201](https://github.com/quarkusio/quarkus/issues/14201) and [14881](https://github.com/quarkusio/quarkus/issues/14881): possible data loss bug in hibernate. This is covered under the Java package `io.quarkus.qe.hibernate.items`. |
37 changes: 37 additions & 0 deletions
37
004-quarkus-HHH-and-HV/src/main/java/io/quarkus/qe/hibernate/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.hibernate.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
004-quarkus-HHH-and-HV/src/main/java/io/quarkus/qe/hibernate/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.hibernate.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
004-quarkus-HHH-and-HV/src/main/java/io/quarkus/qe/hibernate/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.hibernate.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
004-quarkus-HHH-and-HV/src/main/java/io/quarkus/qe/hibernate/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.hibernate.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.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); | ||
// Eager fetch the relationship between item and customer | ||
// Do not remove as this is the actual reproducer for https://github.com/quarkusio/quarkus/issues/14201 and | ||
// https://github.com/quarkusio/quarkus/issues/14881. | ||
cq.from(Item.class).fetch("customer"); | ||
|
||
TypedQuery<Item> query = em.createQuery(cq); | ||
return query.getResultList().size(); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
004-quarkus-HHH-and-HV/src/main/java/io/quarkus/qe/hibernate/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.hibernate.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; | ||
|
||
} |
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
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,5 @@ | ||
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); |
20 changes: 20 additions & 0 deletions
20
004-quarkus-HHH-and-HV/src/test/java/io/quarkus/qe/hibernate/items/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,20 @@ | ||
package io.quarkus.qe.hibernate.items; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
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
...quarkus-HHH-and-HV/src/test/java/io/quarkus/qe/hibernate/items/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.hibernate.items; | ||
|
||
import io.quarkus.test.junit.NativeImageTest; | ||
|
||
@NativeImageTest | ||
public class NativeItemsResourceIT extends ItemsResourceTest { | ||
} |