diff --git a/integration-tests/jpa-db2/src/main/java/io/quarkus/it/jpa/db2/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-db2/src/main/java/io/quarkus/it/jpa/db2/JPAFunctionalityTestEndpoint.java index c840c98c946fa..530eeefe367f9 100644 --- a/integration-tests/jpa-db2/src/main/java/io/quarkus/it/jpa/db2/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-db2/src/main/java/io/quarkus/it/jpa/db2/JPAFunctionalityTestEndpoint.java @@ -6,8 +6,6 @@ import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -17,6 +15,8 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import io.quarkus.narayana.jta.QuarkusTransaction; + /** * Various tests covering JPA functionality. All tests should work in both standard JVM and native mode. */ @@ -25,105 +25,74 @@ public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; + EntityManager em; @GET public String test() throws IOException { - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + sb.append("\n\t"); + if (p.getStatus() != Status.LIVING) { + throw new RuntimeException("Incorrect status " + p); + } + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); + } + }); - deleteAllPerson(entityManagerFactory); + cleanUpData(); return "OK"; } - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } - - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } - - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } - - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from Person").executeUpdate(); - transaction.commit(); - em.close(); - } - - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - sb.append("\n\t"); - if (p.getStatus() != Status.LIVING) { - throw new RuntimeException("Incorrect status " + p); - } - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); person.setStatus(Status.LIVING); person.setAddress(new SequencedAddress("Street " + randomName())); - entityManager.persist(person); + em.persist(person); } private static String randomName() { diff --git a/integration-tests/jpa-derby/src/main/java/io/quarkus/it/jpa/derby/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-derby/src/main/java/io/quarkus/it/jpa/derby/JPAFunctionalityTestEndpoint.java index ceab219e922f3..e996e51eb641b 100644 --- a/integration-tests/jpa-derby/src/main/java/io/quarkus/it/jpa/derby/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-derby/src/main/java/io/quarkus/it/jpa/derby/JPAFunctionalityTestEndpoint.java @@ -5,8 +5,6 @@ import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -16,129 +14,84 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import io.quarkus.narayana.jta.QuarkusTransaction; + /** * Basic test running JPA with the Derby database. - * The application can work in either standard JVM or in native mode, while we run H2 as a separate JVM process. + * The application can work in either standard JVM or in native mode, while we run Derby as a separate JVM process. */ @Path("/jpa-derby/testfunctionality") @Produces(MediaType.TEXT_PLAIN) public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; + EntityManager em; @GET public String test() { - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - verifyHqlFetch(entityManagerFactory); - - deleteAllPerson(entityManagerFactory); - - return "OK"; - } - - private static void verifyHqlFetch(EntityManagerFactory emf) { - try (EntityManager em = emf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - try { - transaction.begin(); - - em.createQuery("from Person p left join fetch p.address a").getResultList(); - - transaction.commit(); - } catch (Exception e) { - if (transaction.isActive()) { - transaction.rollback(); - } - throw e; + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); } - } - } + }); - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } + //Check that HQL fetch does not fail + QuarkusTransaction.requiringNew() + .run(() -> em.createQuery("from Person p left join fetch p.address a").getResultList()); - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } - - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } + cleanUpData(); - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from Person").executeUpdate(); - transaction.commit(); - em.close(); + return "OK"; } - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); person.setAddress(new SequencedAddress("Street " + randomName())); - entityManager.persist(person); + em.persist(person); } private static String randomName() { diff --git a/integration-tests/jpa-h2-embedded/src/main/java/io/quarkus/it/jpa/h2/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-h2-embedded/src/main/java/io/quarkus/it/jpa/h2/JPAFunctionalityTestEndpoint.java index 1e88ef45ae992..6ef09da012468 100644 --- a/integration-tests/jpa-h2-embedded/src/main/java/io/quarkus/it/jpa/h2/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-h2-embedded/src/main/java/io/quarkus/it/jpa/h2/JPAFunctionalityTestEndpoint.java @@ -4,10 +4,8 @@ import java.util.List; import java.util.UUID; +import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; -import jakarta.persistence.PersistenceUnit; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -17,6 +15,8 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import io.quarkus.narayana.jta.QuarkusTransaction; + /** * Basic test running JPA with the H2 database. * The application can work in either standard JVM or in native mode, embedding H2 within the application. @@ -25,121 +25,75 @@ @Produces(MediaType.TEXT_PLAIN) public class JPAFunctionalityTestEndpoint { - @PersistenceUnit - EntityManagerFactory entityManagerFactory; + @Inject + EntityManager em; @GET public String test() throws IOException { - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - verifyHqlFetch(entityManagerFactory); - - deleteAllPerson(entityManagerFactory); - - return "OK"; - } - - private static void verifyHqlFetch(EntityManagerFactory emf) { - try (EntityManager em = emf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - try { - transaction.begin(); - - em.createQuery("from Person p left join fetch p.address a").getResultList(); - - transaction.commit(); - } catch (Exception e) { - if (transaction.isActive()) { - transaction.rollback(); - } - throw e; + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); } - } - } + }); - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } + //Check that HQL fetch does not throw an exception + QuarkusTransaction.requiringNew().run(() -> { + em.createQuery("from Person p left join fetch p.address a").getResultList(); + }); - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } - - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } + cleanUpData(); - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from Person").executeUpdate(); - transaction.commit(); - em.close(); + return "OK"; } - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); person.setAddress(new SequencedAddress("Street " + randomName())); - entityManager.persist(person); + em.persist(person); } private static String randomName() { diff --git a/integration-tests/jpa-h2/src/main/java/io/quarkus/it/jpa/h2/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-h2/src/main/java/io/quarkus/it/jpa/h2/JPAFunctionalityTestEndpoint.java index 2b1874d3e72dc..d77d22d18ee89 100644 --- a/integration-tests/jpa-h2/src/main/java/io/quarkus/it/jpa/h2/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-h2/src/main/java/io/quarkus/it/jpa/h2/JPAFunctionalityTestEndpoint.java @@ -3,12 +3,9 @@ import java.io.IOException; import java.util.List; import java.util.UUID; -import java.util.function.Consumer; import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -18,7 +15,7 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; -import org.wildfly.common.Assert; +import io.quarkus.narayana.jta.QuarkusTransaction; /** * Basic test running JPA with the H2 database. @@ -29,160 +26,78 @@ public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; + EntityManager em; @GET public String test() throws IOException { - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - verifyHqlFetch(entityManagerFactory); - - deleteAllPerson(entityManagerFactory); - - //Test capability to load enhanced proxies: - verifyEnhancedProxies(entityManagerFactory); - - return "OK"; - } - - private static void verifyEnhancedProxies(EntityManagerFactory emf) { - //Define the test data: - CompanyCustomer company = new CompanyCustomer(); - company.companyname = "Quarked consulting, inc."; - Project project = new Project(); - project.name = "Hibernate RX"; - project.customer = company; - - //Store the test model: - doAsUnit(emf, em -> em.persist(project)); - final Integer testId = project.id; - Assert.assertNotNull(testId); - - //Now try to load it, should trigger the use of enhanced proxies: - doAsUnit(emf, em -> em.find(Project.class, testId)); - } - - private static void verifyHqlFetch(EntityManagerFactory emf) { - try (EntityManager em = emf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - try { - transaction.begin(); - - em.createQuery("from Person p left join fetch p.address a").getResultList(); - - transaction.commit(); - } catch (Exception e) { - if (transaction.isActive()) { - transaction.rollback(); - } - throw e; + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); } - } - } + }); - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } + //Check that HQL fetch does not throw an exception + QuarkusTransaction.requiringNew().run(() -> { + em.createQuery("from Person p left join fetch p.address a").getResultList(); + }); - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } + cleanUpData(); - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } - - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from Person").executeUpdate(); - transaction.commit(); - em.close(); + return "OK"; } - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); person.setAddress(new SequencedAddress("Street " + randomName())); - entityManager.persist(person); + em.persist(person); } private static String randomName() { return UUID.randomUUID().toString(); } - private static void doAsUnit(EntityManagerFactory emf, Consumer f) { - try (EntityManager em = emf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - try { - transaction.begin(); - f.accept(em); - transaction.commit(); - } catch (Exception e) { - if (transaction.isActive()) { - transaction.rollback(); - } - throw e; - } - } - } - } diff --git a/integration-tests/jpa-mariadb/src/main/java/io/quarkus/it/jpa/mariadb/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-mariadb/src/main/java/io/quarkus/it/jpa/mariadb/JPAFunctionalityTestEndpoint.java index 88673c30d6cb4..5048195ae3fac 100644 --- a/integration-tests/jpa-mariadb/src/main/java/io/quarkus/it/jpa/mariadb/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-mariadb/src/main/java/io/quarkus/it/jpa/mariadb/JPAFunctionalityTestEndpoint.java @@ -11,8 +11,6 @@ import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -22,6 +20,8 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import io.quarkus.narayana.jta.QuarkusTransaction; + /** * Basic test running JPA with the MariaDB database. * The application can work in either standard JVM or in native mode. @@ -31,124 +31,90 @@ public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; + EntityManager em; @Inject DataSource ds; @GET public String test() throws IOException, SQLException { - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try to use prepared statement with setObject: - verifyGetPersonUsingSetObject(ds); - - //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - deleteAllPerson(entityManagerFactory); - - return "OK"; - } + try (PreparedStatement ps = ds.getConnection().prepareStatement("select * from Person as p where p.name = ?")) { + ps.setObject(1, "Quarkus"); + final ResultSet resultSet = ps.executeQuery(); - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); + if (!resultSet.next()) { + throw new RuntimeException("Person Quarkus doesn't exist when it should"); + } } - transaction.commit(); - em.close(); - } + //Try a JPA named query: + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); + } + }); - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } + //Check that HQL fetch does not throw an exception + QuarkusTransaction.requiringNew().run(() -> { + em.createQuery("from Person p left join fetch p.address a").getResultList(); + }); - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } + cleanUpData(); - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from Person").executeUpdate(); - transaction.commit(); - em.close(); + return "OK"; } - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); person.setAddress(new Address("Street " + randomName())); - entityManager.persist(person); + em.persist(person); } private static String randomName() { return UUID.randomUUID().toString(); } - - private static void verifyGetPersonUsingSetObject(final DataSource ds) throws SQLException { - getExistingPersonUsingSetObject(ds); - } - - private static void getExistingPersonUsingSetObject(DataSource ds) throws SQLException { - try (PreparedStatement ps = ds.getConnection().prepareStatement("select * from Person as p where p.name = ?")) { - ps.setObject(1, "Quarkus"); - final ResultSet resultSet = ps.executeQuery(); - - if (!resultSet.next()) { - throw new RuntimeException("Person Quarkus doesn't exist when it should"); - } - } - } } diff --git a/integration-tests/jpa-mssql/src/main/java/io/quarkus/it/jpa/mssql/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-mssql/src/main/java/io/quarkus/it/jpa/mssql/JPAFunctionalityTestEndpoint.java index 73061a5519079..6a7a8c1a2ccbd 100644 --- a/integration-tests/jpa-mssql/src/main/java/io/quarkus/it/jpa/mssql/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-mssql/src/main/java/io/quarkus/it/jpa/mssql/JPAFunctionalityTestEndpoint.java @@ -7,8 +7,6 @@ import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -18,6 +16,8 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import io.quarkus.narayana.jta.QuarkusTransaction; + /** * Basic test running JPA with the MS SQL database. * The application can work in either standard JVM or in native mode. @@ -27,126 +27,77 @@ public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; + EntityManager em; @GET public String test() throws IOException { if (!Charset.isSupported("Cp1252")) throw new IllegalStateException("You will very likely need support for Codepage Cp1252 to connect to SQL Server"); - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - verifyHqlFetch(entityManagerFactory); - - deleteAllPerson(entityManagerFactory); - - return "OK"; - } - - private static void verifyHqlFetch(EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - try { - EntityTransaction transaction = em.getTransaction(); - try { - transaction.begin(); - - em.createQuery("from Person p left join fetch p.address a").getResultList(); - - transaction.commit(); - } catch (Exception e) { - if (transaction.isActive()) { - transaction.rollback(); - } - throw e; + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); } - } finally { - em.close(); - } - } - - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } + }); - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } + //Check that HQL fetch does not throw an exception + QuarkusTransaction.requiringNew().run(() -> { + em.createQuery("from Person p left join fetch p.address a").getResultList(); + }); - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } + cleanUpData(); - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from Person").executeUpdate(); - transaction.commit(); - em.close(); + return "OK"; } - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); person.setAddress(new SequencedAddress("Street " + randomName())); - entityManager.persist(person); + em.persist(person); } private static String randomName() { diff --git a/integration-tests/jpa-mysql/src/main/java/io/quarkus/it/jpa/mysql/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-mysql/src/main/java/io/quarkus/it/jpa/mysql/JPAFunctionalityTestEndpoint.java index f21d9de96ee05..0c1b74321aca1 100644 --- a/integration-tests/jpa-mysql/src/main/java/io/quarkus/it/jpa/mysql/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-mysql/src/main/java/io/quarkus/it/jpa/mysql/JPAFunctionalityTestEndpoint.java @@ -1,12 +1,11 @@ package io.quarkus.it.jpa.mysql; +import java.io.IOException; import java.util.List; import java.util.UUID; import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -16,6 +15,8 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import io.quarkus.narayana.jta.QuarkusTransaction; + /** * Basic test running JPA with the MySQL database. * The application can work in either standard JVM or in native mode. @@ -25,101 +26,69 @@ public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; + EntityManager em; @GET - public String test() { - - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + public String test() throws IOException { + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - deleteAllPerson(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); - return "OK"; - } + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); + } + }); - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } + cleanUpData(); - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } - - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } - - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from Person").executeUpdate(); - transaction.commit(); - em.close(); + return "OK"; } - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); person.setAddress(new Address("Street " + randomName())); - entityManager.persist(person); + em.persist(person); } private static String randomName() { diff --git a/integration-tests/jpa-oracle/src/main/java/io/quarkus/example/jpaoracle/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-oracle/src/main/java/io/quarkus/example/jpaoracle/JPAFunctionalityTestEndpoint.java index 2a8e49ac2734a..f3723e3b99959 100644 --- a/integration-tests/jpa-oracle/src/main/java/io/quarkus/example/jpaoracle/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-oracle/src/main/java/io/quarkus/example/jpaoracle/JPAFunctionalityTestEndpoint.java @@ -1,12 +1,11 @@ package io.quarkus.example.jpaoracle; +import java.io.IOException; import java.util.List; import java.util.UUID; import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -16,6 +15,8 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; +import io.quarkus.narayana.jta.QuarkusTransaction; + /** * Basic test running JPA with the Oracle database. * The application can work in either standard JVM or SubstrateVM. @@ -25,120 +26,74 @@ public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; + EntityManager em; @GET - public String test() { - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + public String test() throws IOException { + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - verifyHqlFetch(entityManagerFactory); - - deleteAllPerson(entityManagerFactory); - - return "OK"; - } - - private static void verifyHqlFetch(EntityManagerFactory emf) { - try (EntityManager em = emf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - try { - transaction.begin(); - - em.createQuery("from Person p left join fetch p.address a").getResultList(); - - transaction.commit(); - } catch (Exception e) { - if (transaction.isActive()) { - transaction.rollback(); - } - throw e; + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); } - } - } + }); - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } + //Check that HQL fetch does not throw an exception + QuarkusTransaction.requiringNew().run(() -> { + em.createQuery("from Person p left join fetch p.address a").getResultList(); + }); - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } - - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } + cleanUpData(); - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from Person").executeUpdate(); - transaction.commit(); - em.close(); + return "OK"; } - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); person.setAddress(new SequencedAddress("Street " + randomName())); - entityManager.persist(person); + em.persist(person); } private static String randomName() { diff --git a/integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java index 86bb7c2d7449b..20420cfab3df6 100644 --- a/integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java @@ -22,8 +22,6 @@ import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -35,6 +33,7 @@ import io.quarkus.hibernate.orm.PersistenceUnit; import io.quarkus.it.jpa.postgresql.otherpu.EntityWithXmlOtherPU; +import io.quarkus.narayana.jta.QuarkusTransaction; /** * First we run a smoke test for basic Hibernate ORM functionality, @@ -46,28 +45,96 @@ public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; + EntityManager em; @Inject @PersistenceUnit("other") - EntityManagerFactory otherEntityManagerFactory; + EntityManager otherEm; @Inject DataSource ds; - @GET - public String test() throws SQLException, TransformerException { - doStuffWithHibernate(entityManagerFactory, otherEntityManagerFactory); - doStuffWithDatasource(); + @Path("base") + public String base() { + cleanUpData(); + + //Store some well known Person instances we can then test on: + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); + + //Load all persons and run some checks on the query results: + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); + + //Try a JPA named query: + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); + } + }); + + //Check that HQL fetch does not throw an exception + QuarkusTransaction.requiringNew().run(() -> { + em.createQuery("from Person p left join fetch p.address a").getResultList(); + }); + + cleanUpData(); + return "OK"; } - private void doStuffWithDatasource() throws SQLException, TransformerException { + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); + } + + private void persistNewPerson(String name) { + Person person = new Person(); + person.setName(name); + person.setAddress(new SequencedAddress("Street " + randomName())); + em.persist(person); + } + + private static String randomName() { + return UUID.randomUUID().toString(); + } + + @GET + @Path("datasource-xml") + public String datasourceXml() throws SQLException, TransformerException { try (final Connection con = ds.getConnection()) { deleteXmlSchema(con); createXmlSchema(con); writeXmlObject(con); checkWrittenXmlObject(con); } + return "OK"; } private void checkWrittenXmlObject(Connection con) throws SQLException { @@ -122,142 +189,38 @@ private void deleteXmlSchema(Connection con) { } } - /** - * Lists the various operations we want to test for: - */ - private static void doStuffWithHibernate(EntityManagerFactory entityManagerFactory, - EntityManagerFactory otherEntityManagerFactory) { - - //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); - - //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); - - //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - doXmlStuff(entityManagerFactory, otherEntityManagerFactory); - } - - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } - - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } - - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } - - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - sb.append("\n\t"); - if (p.getStatus() != Status.LIVING) { - throw new RuntimeException("Incorrect status " + p); - } - } - sb.append("\nList complete.\n"); - System.out.print(sb); - } - - private static void persistNewPerson(EntityManager entityManager, String name) { - Person person = new Person(); - person.setName(name); - person.setStatus(Status.LIVING); - person.setAddress(new SequencedAddress("Street " + randomName())); - entityManager.persist(person); - } - - private static String randomName() { - return UUID.randomUUID().toString(); - } - - private static void doXmlStuff(EntityManagerFactory emf, EntityManagerFactory otherEmf) { - try (EntityManager em = emf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - + @GET + @Path("hibernate-xml") + public String hibernateXml() { + QuarkusTransaction.requiringNew().run(() -> { EntityWithXml entity = new EntityWithXml( new EntityWithXml.ToBeSerializedWithDateTime(LocalDate.of(2023, 7, 28))); - em.persist(entity); - transaction.commit(); - } + otherEm.persist(entity); + }); - try (EntityManager em = emf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - List entities = em + QuarkusTransaction.requiringNew().run(() -> { + List entities = otherEm .createQuery("select e from EntityWithXml e", EntityWithXml.class) .getResultList(); if (entities.isEmpty()) { throw new AssertionError("No entities with XML were found"); } - transaction.commit(); - } + }); - try (EntityManager em = emf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createQuery("delete from EntityWithXml").executeUpdate(); - transaction.commit(); - } + QuarkusTransaction.requiringNew().run(() -> { + otherEm.createQuery("delete from EntityWithXml").executeUpdate(); + }); - try (EntityManager em = otherEmf.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); + QuarkusTransaction.requiringNew().run(() -> { EntityWithXmlOtherPU otherPU = new EntityWithXmlOtherPU( new EntityWithXmlOtherPU.ToBeSerializedWithDateTime(LocalDate.of(2023, 7, 28))); Exception exception = null; try { - em.persist(otherPU); - em.flush(); + otherEm.persist(otherPU); + otherEm.flush(); } catch (Exception e) { exception = e; } - transaction.rollback(); if (exception == null) { throw new AssertionError( @@ -267,7 +230,9 @@ private static void doXmlStuff(EntityManagerFactory emf, EntityManagerFactory ot || !exception.getMessage().contains("I cannot convert anything to XML")) { throw new AssertionError("flush failed for a different reason than expected.", exception); } - } + }); + + return "OK"; } } diff --git a/integration-tests/jpa-postgresql-withxml/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java b/integration-tests/jpa-postgresql-withxml/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java index d7ade11f80c3c..75f9943714d45 100644 --- a/integration-tests/jpa-postgresql-withxml/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java +++ b/integration-tests/jpa-postgresql-withxml/src/test/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTest.java @@ -14,8 +14,18 @@ public class JPAFunctionalityTest { @Test - public void testJPAFunctionalityFromServlet() throws Exception { - RestAssured.when().get("/jpa-withxml/testfunctionality").then().body(is("OK")); + public void testBase() throws Exception { + RestAssured.when().get("/jpa-withxml/testfunctionality/base").then().body(is("OK")); + } + + @Test + public void testDatasourceXml() throws Exception { + RestAssured.when().get("/jpa-withxml/testfunctionality/datasource-xml").then().body(is("OK")); + } + + @Test + public void testHibernateXml() throws Exception { + RestAssured.when().get("/jpa-withxml/testfunctionality/hibernate-xml").then().body(is("OK")); } } diff --git a/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java b/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java index 63cb03c48644c..1f0472d3ac864 100644 --- a/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java +++ b/integration-tests/jpa-postgresql/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java @@ -1,14 +1,11 @@ package io.quarkus.it.jpa.postgresql; -import java.time.Duration; import java.time.LocalDate; import java.util.List; import java.util.UUID; import jakarta.inject.Inject; import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -18,8 +15,8 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; -import io.quarkus.hibernate.orm.PersistenceUnit; import io.quarkus.it.jpa.postgresql.otherpu.EntityWithJsonOtherPU; +import io.quarkus.narayana.jta.QuarkusTransaction; /** * Various tests covering JPA functionality. All tests should work in both standard JVM and in native mode. @@ -29,110 +26,74 @@ public class JPAFunctionalityTestEndpoint { @Inject - EntityManagerFactory entityManagerFactory; - @Inject - @PersistenceUnit("other") - EntityManagerFactory otherEntityManagerFactory; + EntityManager em; - @GET @Path("base") public String base() { - //Cleanup any existing data: - deleteAllPerson(entityManagerFactory); + cleanUpData(); //Store some well known Person instances we can then test on: - storeTestPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + persistNewPerson("Gizmo"); + persistNewPerson("Quarkus"); + persistNewPerson("Hibernate ORM"); + }); //Load all persons and run some checks on the query results: - verifyListOfExistingPersons(entityManagerFactory); + QuarkusTransaction.requiringNew().run(() -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createQuery(Person.class); + Root from = cq.from(Person.class); + cq.select(from).orderBy(cb.asc(from.get("name"))); + TypedQuery q = em.createQuery(cq); + List allpersons = q.getResultList(); + if (allpersons.size() != 3) { + throw new RuntimeException("Incorrect number of results"); + } + if (!allpersons.get(0).getName().equals("Gizmo")) { + throw new RuntimeException("Incorrect order of results"); + } + StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); + for (Person p : allpersons) { + p.describeFully(sb); + } + sb.append("\nList complete.\n"); + System.out.print(sb); + }); //Try a JPA named query: - verifyJPANamedQuery(entityManagerFactory); - - deleteAllPerson(entityManagerFactory); - - return "OK"; - } + QuarkusTransaction.requiringNew().run(() -> { + TypedQuery typedQuery = em.createNamedQuery( + "get_person_by_name", Person.class); + typedQuery.setParameter("name", "Quarkus"); + final Person singleResult = typedQuery.getSingleResult(); + + if (!singleResult.getName().equals("Quarkus")) { + throw new RuntimeException("Wrong result from named JPA query"); + } + }); - private static void verifyJPANamedQuery(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - TypedQuery typedQuery = em.createNamedQuery( - "get_person_by_name", Person.class); - typedQuery.setParameter("name", "Quarkus"); - final Person singleResult = typedQuery.getSingleResult(); - - if (!singleResult.getName().equals("Quarkus")) { - throw new RuntimeException("Wrong result from named JPA query"); - } - - transaction.commit(); - em.close(); - } + //Check that HQL fetch does not throw an exception + QuarkusTransaction.requiringNew().run(() -> { + em.createQuery("from Person p left join fetch p.address a").getResultList(); + }); - private static void verifyListOfExistingPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - listExistingPersons(em); - transaction.commit(); - em.close(); - } - - private static void storeTestPersons(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - persistNewPerson(em, "Gizmo"); - persistNewPerson(em, "Quarkus"); - persistNewPerson(em, "Hibernate ORM"); - transaction.commit(); - em.close(); - } + cleanUpData(); - private static void deleteAllPerson(final EntityManagerFactory emf) { - EntityManager em = emf.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - em.createNativeQuery("Delete from myschema.Person").executeUpdate(); - transaction.commit(); - em.close(); + return "OK"; } - private static void listExistingPersons(EntityManager em) { - CriteriaBuilder cb = em.getCriteriaBuilder(); - - CriteriaQuery cq = cb.createQuery(Person.class); - Root from = cq.from(Person.class); - cq.select(from).orderBy(cb.asc(from.get("name"))); - TypedQuery q = em.createQuery(cq); - List allpersons = q.getResultList(); - if (allpersons.size() != 3) { - throw new RuntimeException("Incorrect number of results"); - } - if (!allpersons.get(0).getName().equals("Gizmo")) { - throw new RuntimeException("Incorrect order of results"); - } - StringBuilder sb = new StringBuilder("list of stored Person names:\n\t"); - for (Person p : allpersons) { - p.describeFully(sb); - sb.append("\n\t"); - if (p.getStatus() != Status.LIVING) { - throw new RuntimeException("Incorrect status " + p); - } - } - sb.append("\nList complete.\n"); - System.out.print(sb); + private void cleanUpData() { + QuarkusTransaction.requiringNew() + .run(() -> em.createNativeQuery("Delete from Person").executeUpdate()); } - private static void persistNewPerson(EntityManager entityManager, String name) { + private void persistNewPerson(String name) { Person person = new Person(); person.setName(name); - person.setStatus(Status.LIVING); person.setAddress(new SequencedAddress("Street " + randomName())); - person.setLatestLunchBreakDuration(Duration.ofMinutes(30)); - entityManager.persist(person); + em.persist(person); } private static String randomName() { @@ -142,62 +103,45 @@ private static String randomName() { @GET @Path("uuid") public String uuid() { - EntityManager em = entityManagerFactory.createEntityManager(); - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - MyUUIDEntity myEntity = new MyUUIDEntity(); - myEntity.setName("George"); - em.persist(myEntity); - transaction.commit(); - em.close(); - - em = entityManagerFactory.createEntityManager(); - transaction = em.getTransaction(); - transaction.begin(); - myEntity = em.find(MyUUIDEntity.class, myEntity.getId()); - if (myEntity == null || !"George".equals(myEntity.getName())) { - throw new RuntimeException("Incorrect loaded MyUUIDEntity " + myEntity); - } - transaction.commit(); - em.close(); + var id = QuarkusTransaction.requiringNew().call(() -> { + MyUUIDEntity myEntity = new MyUUIDEntity(); + myEntity.setName("George"); + em.persist(myEntity); + return myEntity.getId(); + }); + + QuarkusTransaction.requiringNew().run(() -> { + var myEntity = em.find(MyUUIDEntity.class, id); + if (myEntity == null || !"George".equals(myEntity.getName())) { + throw new RuntimeException("Incorrect loaded MyUUIDEntity " + myEntity); + } + }); return "OK"; } @GET @Path("json") public String json() { - try (EntityManager em = entityManagerFactory.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); - + QuarkusTransaction.requiringNew().run(() -> { EntityWithJson entity = new EntityWithJson( new EntityWithJson.ToBeSerializedWithDateTime(LocalDate.of(2023, 7, 28))); em.persist(entity); - transaction.commit(); - } + }); - try (EntityManager em = entityManagerFactory.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); + QuarkusTransaction.requiringNew().run(() -> { List entities = em .createQuery("select e from EntityWithJson e", EntityWithJson.class) .getResultList(); if (entities.isEmpty()) { throw new AssertionError("No entities with json were found"); } - transaction.commit(); - } + }); - try (EntityManager em = entityManagerFactory.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); + QuarkusTransaction.requiringNew().run(() -> { em.createQuery("delete from EntityWithJson").executeUpdate(); - transaction.commit(); - } + }); - try (EntityManager em = otherEntityManagerFactory.createEntityManager()) { - EntityTransaction transaction = em.getTransaction(); - transaction.begin(); + QuarkusTransaction.requiringNew().run(() -> { EntityWithJsonOtherPU otherPU = new EntityWithJsonOtherPU( new EntityWithJsonOtherPU.ToBeSerializedWithDateTime(LocalDate.of(2023, 7, 28))); Exception exception = null; @@ -207,7 +151,6 @@ public String json() { } catch (Exception e) { exception = e; } - transaction.rollback(); if (exception == null) { throw new AssertionError( @@ -217,7 +160,7 @@ public String json() { || !exception.getMessage().contains("I cannot convert anything to JSON")) { throw new AssertionError("flush failed for a different reason than expected.", exception); } - } + }); return "OK"; }