From 6f0dba94ce71b8ee276224ddac342c09152882f8 Mon Sep 17 00:00:00 2001 From: Sumeet Phadnis Date: Thu, 21 Nov 2024 18:08:18 +0530 Subject: [PATCH] Added support for connection pool managers with Hibernate Both c3p0 and hicaricp are supported --- gradle/libs.versions.toml | 12 ++++++--- modules/dbsupport/build.gradle | 2 +- .../src/main/java/org/jpos/ee/DBManager.java | 25 +++++++------------ 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index edb286963..827e0b6f4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,7 +13,7 @@ freemarker = '2.3.32' jsonSchemaValidator = '2.2.14' guava = '33.0.0-jre' jakartaActivation = '2.0.1' -hibernate = '6.6.1.Final' +hibernate = '6.6.2.Final' dom4j = '2.1.4' jdbcPostgresql = '42.7.2' jdbcMysql = '8.0.33' @@ -44,10 +44,12 @@ freemarker = { module = 'org.freemarker:freemarker', version.ref = 'freemarker' jsonSchemaValidator = { module = 'com.github.java-json-tools:json-schema-validator', version.ref = 'jsonSchemaValidator' } guava = { module = 'com.google.guava:guava', version.ref = 'guava' } jakartaActivation = { module = 'com.sun.activation:jakarta.activation', version.ref = 'jakartaActivation' } -hibernate = { module = 'org.hibernate:hibernate-core', version.ref='hibernate' } +hibernateCore = { module = 'org.hibernate.orm:hibernate-core', version.ref='hibernate' } hibernateToolsOrm = { module='org.hibernate.tool:hibernate-tools-orm', version.ref='hibernate' } hibernateToolsUtils = { module='org.hibernate.tool:hibernate-tools-utils', version.ref='hibernate' } hibernateAgroal = { module='org.hibernate:hibernate-agroal', version.ref='hibernate' } +hibernateC3p0 = { module='org.hibernate.orm:hibernate-c3p0', version.ref='hibernate' } +hibernateHikari = { module='org.hibernate.orm:hibernate-hikaricp', version.ref='hibernate' } dom4j = { module = 'org.dom4j:dom4j', version.ref='dom4j' } jdbcPostgresql = { module = 'org.postgresql:postgresql', version.ref='jdbcPostgresql' } jdbcMysql = { module = 'mysql:mysql-connector-java', version.ref='jdbcMysql' } @@ -65,4 +67,8 @@ jackson = [ "jacksonDatabind", "jacksonDataTypeJSR310" ] - +hibernate = [ + "hibernateCore", + "hibernateC3p0", + "hibernateHikari" +] diff --git a/modules/dbsupport/build.gradle b/modules/dbsupport/build.gradle index 962504f25..998561226 100644 --- a/modules/dbsupport/build.gradle +++ b/modules/dbsupport/build.gradle @@ -2,7 +2,7 @@ description = 'jPOS-EE :: Database Support Module' dependencies { api project(':modules:core') - api libs.hibernate + api libs.bundles.hibernate api libs.jacksonDatabind api libs.dom4j implementation libs.hibernateToolsOrm diff --git a/modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java b/modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java index 0e85a89d5..ce8dad17c 100644 --- a/modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java +++ b/modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java @@ -18,15 +18,11 @@ package org.jpos.ee; -// import org.hibernate.query.criteria.internal.OrderImpl; - import jakarta.persistence.NoResultException; import jakarta.persistence.criteria.*; import org.hibernate.query.Query; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; + +import java.util.*; public class DBManager { @@ -56,12 +52,11 @@ public int getItemCount() { public int getItemCount(DBFilter... filters) { CriteriaBuilder cb = db.session().getCriteriaBuilder(); - // CriteriaQuery query = createCriteriaQuery(false, (DBFilter) null); CriteriaQuery countQuery = cb.createQuery(Long.class); Root root = countQuery.from(clazz); Predicate combinedPredicate = cb.and( Arrays.stream(filters) - .filter(f -> f != null) + .filter(Objects::nonNull) .map(f -> f.createPredicate(cb, root)) .toArray(Predicate[]::new) ); @@ -80,7 +75,7 @@ public int getItemCount(DBFilter... filters) { } public List getAll(int offset, int limit, Map orders) { - return this.getAll(offset, limit, orders, null); + return this.getAll(offset, limit, orders, (DBFilter) null); } public List getAll(int offset, int limit, Map orders, DBFilter... filters) { @@ -91,13 +86,13 @@ public List getAll(int offset, int limit, Map orders, DBFilte //ORDERS if (orders != null) { orders.forEach((key, value) -> - orderList.add(value ? criteriaBuilder.asc(root.get(key)) : criteriaBuilder.desc(root.get(key))) + orderList.add(Boolean.TRUE.equals(value) ? criteriaBuilder.asc(root.get(key)) : criteriaBuilder.desc(root.get(key))) ); } Predicate combinedPredicate = null; if (filters != null) { combinedPredicate = criteriaBuilder.and(Arrays.stream(filters) - .filter(f -> f != null) + .filter(Objects::nonNull) .map(f -> f.createPredicate(criteriaBuilder, root)) .toArray(Predicate[]::new)); } @@ -115,10 +110,9 @@ public List getAll(int offset, int limit, Map orders, DBFilte if (limit != -1) { queryImp.setMaxResults(limit); } - List list = queryImp + return queryImp .setFirstResult(offset) .getResultList(); - return list; } public List getAll() { @@ -155,10 +149,9 @@ public List getItemsByParam(int offset, int limit, String param, Object value if (limit != -1) { queryImp.setMaxResults(limit); } - List list = queryImp + return queryImp .setFirstResult(offset) .getResultList(); - return list; } catch (NoResultException nre) { return null; } @@ -211,7 +204,7 @@ public List queryItems(DBFilter filter) { * Arbitrary query over the entity type (T) of this manager * Example usage: *
-     *     List≤T&ge results = queryItems( (cb, root) ->
+     *     List≤T≥ results = queryItems( (cb, root) ->
      *          cb.or(
      *              cb.greaterThanOrEqualTo(root.get("property"), value),
      *              cb.isNotNull(root.get("otherProperty")