From 4d93350da6b3902fb1fe368b07f41fc2b03c20ef Mon Sep 17 00:00:00 2001 From: ackasaber Date: Sat, 2 Oct 2021 21:55:03 +0300 Subject: [PATCH] Extracted the helper class JPAUnit into a separate library for reuse. --- jpa02/pom.xml | 5 ++ .../example/jpaunit/JPATransactionAction.java | 7 --- jpa03/pom.xml | 5 ++ .../example/jpaunit/JPATransactionAction.java | 7 --- .../main/java/example/jpaunit/JPAUnit.java | 46 ------------------- jpa04/pom.xml | 5 ++ .../example/jpaunit/JPATransactionAction.java | 7 --- .../main/java/example/jpaunit/JPAUnit.java | 46 ------------------- jpa05/pom.xml | 5 ++ .../example/jpaunit/JPATransactionAction.java | 7 --- .../main/java/example/jpaunit/JPAUnit.java | 46 ------------------- jpa06/pom.xml | 5 ++ .../example/jpaunit/JPATransactionAction.java | 7 --- .../main/java/example/jpaunit/JPAUnit.java | 46 ------------------- jpa07/pom.xml | 5 ++ .../example/jpaunit/JPATransactionAction.java | 7 --- .../main/java/example/jpaunit/JPAUnit.java | 46 ------------------- jpa08/pom.xml | 5 ++ .../main/java/example/jpaunit/JPAUnit.java | 46 ------------------- jpaunit/pom.xml | 20 ++++++++ .../example/jpaunit/JPATransactionAction.java | 0 .../main/java/example/jpaunit/JPAUnit.java | 0 22 files changed, 55 insertions(+), 318 deletions(-) delete mode 100644 jpa02/src/main/java/example/jpaunit/JPATransactionAction.java delete mode 100644 jpa03/src/main/java/example/jpaunit/JPATransactionAction.java delete mode 100644 jpa03/src/main/java/example/jpaunit/JPAUnit.java delete mode 100644 jpa04/src/main/java/example/jpaunit/JPATransactionAction.java delete mode 100644 jpa04/src/main/java/example/jpaunit/JPAUnit.java delete mode 100644 jpa05/src/main/java/example/jpaunit/JPATransactionAction.java delete mode 100644 jpa05/src/main/java/example/jpaunit/JPAUnit.java delete mode 100644 jpa06/src/main/java/example/jpaunit/JPATransactionAction.java delete mode 100644 jpa06/src/main/java/example/jpaunit/JPAUnit.java delete mode 100644 jpa07/src/main/java/example/jpaunit/JPATransactionAction.java delete mode 100644 jpa07/src/main/java/example/jpaunit/JPAUnit.java delete mode 100644 jpa08/src/main/java/example/jpaunit/JPAUnit.java create mode 100644 jpaunit/pom.xml rename {jpa08 => jpaunit}/src/main/java/example/jpaunit/JPATransactionAction.java (100%) rename {jpa02 => jpaunit}/src/main/java/example/jpaunit/JPAUnit.java (100%) diff --git a/jpa02/pom.xml b/jpa02/pom.xml index 04c9f27..b82b670 100644 --- a/jpa02/pom.xml +++ b/jpa02/pom.xml @@ -31,5 +31,10 @@ mariadb-java-client 2.7.4 + + example + jpaunit + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/jpa02/src/main/java/example/jpaunit/JPATransactionAction.java b/jpa02/src/main/java/example/jpaunit/JPATransactionAction.java deleted file mode 100644 index 4b49801..0000000 --- a/jpa02/src/main/java/example/jpaunit/JPATransactionAction.java +++ /dev/null @@ -1,7 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; - -public interface JPATransactionAction { - void run(EntityManager entityManager); -} diff --git a/jpa03/pom.xml b/jpa03/pom.xml index 420c5f6..b2d9608 100644 --- a/jpa03/pom.xml +++ b/jpa03/pom.xml @@ -26,5 +26,10 @@ postgresql 42.2.23.jre7 + + example + jpaunit + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/jpa03/src/main/java/example/jpaunit/JPATransactionAction.java b/jpa03/src/main/java/example/jpaunit/JPATransactionAction.java deleted file mode 100644 index 4b49801..0000000 --- a/jpa03/src/main/java/example/jpaunit/JPATransactionAction.java +++ /dev/null @@ -1,7 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; - -public interface JPATransactionAction { - void run(EntityManager entityManager); -} diff --git a/jpa03/src/main/java/example/jpaunit/JPAUnit.java b/jpa03/src/main/java/example/jpaunit/JPAUnit.java deleted file mode 100644 index 7b2c073..0000000 --- a/jpa03/src/main/java/example/jpaunit/JPAUnit.java +++ /dev/null @@ -1,46 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; -import jakarta.persistence.Persistence; - -public class JPAUnit implements AutoCloseable { - - private EntityManagerFactory emFactory; - - public JPAUnit(String unitName) { - emFactory = Persistence.createEntityManagerFactory(unitName); - } - - public void runTransaction(JPATransactionAction action) { - EntityManager entityManager = null; - - try { - entityManager = emFactory.createEntityManager(); - EntityTransaction transaction = null; - - try { - transaction = entityManager.getTransaction(); - transaction.begin(); - action.run(entityManager); - transaction.commit(); - } catch (Exception e) { - if (transaction != null) { - transaction.rollback(); - } - throw e; - } - } finally { - if (entityManager != null) { - entityManager.close(); - } - } - } - - @Override - public void close() { - emFactory.close(); - } - -} diff --git a/jpa04/pom.xml b/jpa04/pom.xml index 150c0ab..c1802ae 100644 --- a/jpa04/pom.xml +++ b/jpa04/pom.xml @@ -26,5 +26,10 @@ postgresql 42.2.23.jre7 + + example + jpaunit + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/jpa04/src/main/java/example/jpaunit/JPATransactionAction.java b/jpa04/src/main/java/example/jpaunit/JPATransactionAction.java deleted file mode 100644 index 4b49801..0000000 --- a/jpa04/src/main/java/example/jpaunit/JPATransactionAction.java +++ /dev/null @@ -1,7 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; - -public interface JPATransactionAction { - void run(EntityManager entityManager); -} diff --git a/jpa04/src/main/java/example/jpaunit/JPAUnit.java b/jpa04/src/main/java/example/jpaunit/JPAUnit.java deleted file mode 100644 index 7b2c073..0000000 --- a/jpa04/src/main/java/example/jpaunit/JPAUnit.java +++ /dev/null @@ -1,46 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; -import jakarta.persistence.Persistence; - -public class JPAUnit implements AutoCloseable { - - private EntityManagerFactory emFactory; - - public JPAUnit(String unitName) { - emFactory = Persistence.createEntityManagerFactory(unitName); - } - - public void runTransaction(JPATransactionAction action) { - EntityManager entityManager = null; - - try { - entityManager = emFactory.createEntityManager(); - EntityTransaction transaction = null; - - try { - transaction = entityManager.getTransaction(); - transaction.begin(); - action.run(entityManager); - transaction.commit(); - } catch (Exception e) { - if (transaction != null) { - transaction.rollback(); - } - throw e; - } - } finally { - if (entityManager != null) { - entityManager.close(); - } - } - } - - @Override - public void close() { - emFactory.close(); - } - -} diff --git a/jpa05/pom.xml b/jpa05/pom.xml index b5ecc64..9994b2e 100644 --- a/jpa05/pom.xml +++ b/jpa05/pom.xml @@ -26,5 +26,10 @@ postgresql 42.2.23.jre7 + + example + jpaunit + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/jpa05/src/main/java/example/jpaunit/JPATransactionAction.java b/jpa05/src/main/java/example/jpaunit/JPATransactionAction.java deleted file mode 100644 index 4b49801..0000000 --- a/jpa05/src/main/java/example/jpaunit/JPATransactionAction.java +++ /dev/null @@ -1,7 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; - -public interface JPATransactionAction { - void run(EntityManager entityManager); -} diff --git a/jpa05/src/main/java/example/jpaunit/JPAUnit.java b/jpa05/src/main/java/example/jpaunit/JPAUnit.java deleted file mode 100644 index 7b2c073..0000000 --- a/jpa05/src/main/java/example/jpaunit/JPAUnit.java +++ /dev/null @@ -1,46 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; -import jakarta.persistence.Persistence; - -public class JPAUnit implements AutoCloseable { - - private EntityManagerFactory emFactory; - - public JPAUnit(String unitName) { - emFactory = Persistence.createEntityManagerFactory(unitName); - } - - public void runTransaction(JPATransactionAction action) { - EntityManager entityManager = null; - - try { - entityManager = emFactory.createEntityManager(); - EntityTransaction transaction = null; - - try { - transaction = entityManager.getTransaction(); - transaction.begin(); - action.run(entityManager); - transaction.commit(); - } catch (Exception e) { - if (transaction != null) { - transaction.rollback(); - } - throw e; - } - } finally { - if (entityManager != null) { - entityManager.close(); - } - } - } - - @Override - public void close() { - emFactory.close(); - } - -} diff --git a/jpa06/pom.xml b/jpa06/pom.xml index 9bff4ff..e1680c4 100644 --- a/jpa06/pom.xml +++ b/jpa06/pom.xml @@ -26,5 +26,10 @@ postgresql 42.2.23.jre7 + + example + jpaunit + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/jpa06/src/main/java/example/jpaunit/JPATransactionAction.java b/jpa06/src/main/java/example/jpaunit/JPATransactionAction.java deleted file mode 100644 index 4b49801..0000000 --- a/jpa06/src/main/java/example/jpaunit/JPATransactionAction.java +++ /dev/null @@ -1,7 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; - -public interface JPATransactionAction { - void run(EntityManager entityManager); -} diff --git a/jpa06/src/main/java/example/jpaunit/JPAUnit.java b/jpa06/src/main/java/example/jpaunit/JPAUnit.java deleted file mode 100644 index 7b2c073..0000000 --- a/jpa06/src/main/java/example/jpaunit/JPAUnit.java +++ /dev/null @@ -1,46 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; -import jakarta.persistence.Persistence; - -public class JPAUnit implements AutoCloseable { - - private EntityManagerFactory emFactory; - - public JPAUnit(String unitName) { - emFactory = Persistence.createEntityManagerFactory(unitName); - } - - public void runTransaction(JPATransactionAction action) { - EntityManager entityManager = null; - - try { - entityManager = emFactory.createEntityManager(); - EntityTransaction transaction = null; - - try { - transaction = entityManager.getTransaction(); - transaction.begin(); - action.run(entityManager); - transaction.commit(); - } catch (Exception e) { - if (transaction != null) { - transaction.rollback(); - } - throw e; - } - } finally { - if (entityManager != null) { - entityManager.close(); - } - } - } - - @Override - public void close() { - emFactory.close(); - } - -} diff --git a/jpa07/pom.xml b/jpa07/pom.xml index 8b3fc54..be878af 100644 --- a/jpa07/pom.xml +++ b/jpa07/pom.xml @@ -26,5 +26,10 @@ postgresql 42.2.23.jre7 + + example + jpaunit + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/jpa07/src/main/java/example/jpaunit/JPATransactionAction.java b/jpa07/src/main/java/example/jpaunit/JPATransactionAction.java deleted file mode 100644 index 4b49801..0000000 --- a/jpa07/src/main/java/example/jpaunit/JPATransactionAction.java +++ /dev/null @@ -1,7 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; - -public interface JPATransactionAction { - void run(EntityManager entityManager); -} diff --git a/jpa07/src/main/java/example/jpaunit/JPAUnit.java b/jpa07/src/main/java/example/jpaunit/JPAUnit.java deleted file mode 100644 index 7b2c073..0000000 --- a/jpa07/src/main/java/example/jpaunit/JPAUnit.java +++ /dev/null @@ -1,46 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; -import jakarta.persistence.Persistence; - -public class JPAUnit implements AutoCloseable { - - private EntityManagerFactory emFactory; - - public JPAUnit(String unitName) { - emFactory = Persistence.createEntityManagerFactory(unitName); - } - - public void runTransaction(JPATransactionAction action) { - EntityManager entityManager = null; - - try { - entityManager = emFactory.createEntityManager(); - EntityTransaction transaction = null; - - try { - transaction = entityManager.getTransaction(); - transaction.begin(); - action.run(entityManager); - transaction.commit(); - } catch (Exception e) { - if (transaction != null) { - transaction.rollback(); - } - throw e; - } - } finally { - if (entityManager != null) { - entityManager.close(); - } - } - } - - @Override - public void close() { - emFactory.close(); - } - -} diff --git a/jpa08/pom.xml b/jpa08/pom.xml index 99d2eb0..b84197e 100644 --- a/jpa08/pom.xml +++ b/jpa08/pom.xml @@ -26,5 +26,10 @@ postgresql 42.2.23.jre7 + + example + jpaunit + 1.0-SNAPSHOT + \ No newline at end of file diff --git a/jpa08/src/main/java/example/jpaunit/JPAUnit.java b/jpa08/src/main/java/example/jpaunit/JPAUnit.java deleted file mode 100644 index 7b2c073..0000000 --- a/jpa08/src/main/java/example/jpaunit/JPAUnit.java +++ /dev/null @@ -1,46 +0,0 @@ -package example.jpaunit; - -import jakarta.persistence.EntityManager; -import jakarta.persistence.EntityManagerFactory; -import jakarta.persistence.EntityTransaction; -import jakarta.persistence.Persistence; - -public class JPAUnit implements AutoCloseable { - - private EntityManagerFactory emFactory; - - public JPAUnit(String unitName) { - emFactory = Persistence.createEntityManagerFactory(unitName); - } - - public void runTransaction(JPATransactionAction action) { - EntityManager entityManager = null; - - try { - entityManager = emFactory.createEntityManager(); - EntityTransaction transaction = null; - - try { - transaction = entityManager.getTransaction(); - transaction.begin(); - action.run(entityManager); - transaction.commit(); - } catch (Exception e) { - if (transaction != null) { - transaction.rollback(); - } - throw e; - } - } finally { - if (entityManager != null) { - entityManager.close(); - } - } - } - - @Override - public void close() { - emFactory.close(); - } - -} diff --git a/jpaunit/pom.xml b/jpaunit/pom.xml new file mode 100644 index 0000000..6fc9e72 --- /dev/null +++ b/jpaunit/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + example + jpaunit + 1.0-SNAPSHOT + jar + + UTF-8 + 17 + 17 + + + + jakarta.persistence + jakarta.persistence-api + 3.0.0 + + + \ No newline at end of file diff --git a/jpa08/src/main/java/example/jpaunit/JPATransactionAction.java b/jpaunit/src/main/java/example/jpaunit/JPATransactionAction.java similarity index 100% rename from jpa08/src/main/java/example/jpaunit/JPATransactionAction.java rename to jpaunit/src/main/java/example/jpaunit/JPATransactionAction.java diff --git a/jpa02/src/main/java/example/jpaunit/JPAUnit.java b/jpaunit/src/main/java/example/jpaunit/JPAUnit.java similarity index 100% rename from jpa02/src/main/java/example/jpaunit/JPAUnit.java rename to jpaunit/src/main/java/example/jpaunit/JPAUnit.java