Skip to content

Commit

Permalink
Added support for connection pool managers with Hibernate
Browse files Browse the repository at this point in the history
Both c3p0 and hicaricp are supported
  • Loading branch information
sumeetphadnis committed Nov 21, 2024
1 parent a83854f commit 6f0dba9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
12 changes: 9 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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' }
Expand All @@ -65,4 +67,8 @@ jackson = [
"jacksonDatabind",
"jacksonDataTypeJSR310"
]

hibernate = [
"hibernateCore",
"hibernateC3p0",
"hibernateHikari"
]
2 changes: 1 addition & 1 deletion modules/dbsupport/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 9 additions & 16 deletions modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {

Expand Down Expand Up @@ -56,12 +52,11 @@ public int getItemCount() {

public int getItemCount(DBFilter<T>... filters) {
CriteriaBuilder cb = db.session().getCriteriaBuilder();
// CriteriaQuery<Card> query = createCriteriaQuery(false, (DBFilter<Card>) null);
CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
Root<T> 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)
);
Expand All @@ -80,7 +75,7 @@ public int getItemCount(DBFilter<T>... filters) {

}
public List<T> getAll(int offset, int limit, Map<String,Boolean> orders) {
return this.getAll(offset, limit, orders, null);
return this.getAll(offset, limit, orders, (DBFilter<T>) null);
}

public List<T> getAll(int offset, int limit, Map<String,Boolean> orders, DBFilter<T>... filters) {
Expand All @@ -91,13 +86,13 @@ public List<T> getAll(int offset, int limit, Map<String,Boolean> 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));
}
Expand All @@ -115,10 +110,9 @@ public List<T> getAll(int offset, int limit, Map<String,Boolean> orders, DBFilte
if (limit != -1) {
queryImp.setMaxResults(limit);
}
List<T> list = queryImp
return queryImp
.setFirstResult(offset)
.getResultList();
return list;
}

public List<T> getAll() {
Expand Down Expand Up @@ -155,10 +149,9 @@ public List<T> getItemsByParam(int offset, int limit, String param, Object value
if (limit != -1) {
queryImp.setMaxResults(limit);
}
List<T> list = queryImp
return queryImp
.setFirstResult(offset)
.getResultList();
return list;
} catch (NoResultException nre) {
return null;
}
Expand Down Expand Up @@ -211,7 +204,7 @@ public List<T> queryItems(DBFilter<T> filter) {
* Arbitrary query over the entity type (T) of this manager
* Example usage:
* <pre>
* List&le;T&ge results = queryItems( (cb, root) ->
* List&le;T&ge; results = queryItems( (cb, root) ->
* cb.or(
* cb.greaterThanOrEqualTo(root.get("property"), value),
* cb.isNotNull(root.get("otherProperty")
Expand Down

0 comments on commit 6f0dba9

Please sign in to comment.