Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature update to hibernate 6 and to jakarta ee #272

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
12 changes: 7 additions & 5 deletions libraries.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ ext {
jposVersion = '2.1.9-SNAPSHOT'
slf4jVersion = '1.7.28'
logbackVersion = '1.2.8'
hibernateVersion = '5.6.11.Final'
hibernateVersion = '6.1.5.Final'
hibernateEhcacheVersion = '6.0.0.Alpha7'
geronimoVersion = '1.1.1'
jettyVersion = '9.4.50.v20221201'
servletApiVersion = '4.0.1'
Expand Down Expand Up @@ -56,11 +57,12 @@ ext {
joda_time: 'joda-time:joda-time:2.10.12',

//Hibernate
hibernate_core: "org.hibernate:hibernate-core:${hibernateVersion}",
hibernate_envers: "org.hibernate:hibernate-envers:${hibernateVersion}",
hibernate_c3p0: "org.hibernate:hibernate-c3p0:${hibernateVersion}",
hibernate_core: "org.hibernate.orm:hibernate-core:${hibernateVersion}",
hibernate_envers: "org.hibernate.orm:hibernate-envers:${hibernateVersion}",
hibernate_c3p0: "org.hibernate.orm:hibernate-c3p0:${hibernateVersion}",
hibernate_ant: "org.hibernate.orm:hibernate-ant:${hibernateVersion}",
c3p0: "com.mchange:c3p0:${c3p0Version}",
hibernate_ehcache: "org.hibernate:hibernate-ehcache:${hibernateVersion}",
hibernate_ehcache: "org.hibernate:hibernate-ehcache:${hibernateEhcacheVersion}",
jta: "org.apache.geronimo.specs:geronimo-jta_1.1_spec:${geronimoVersion}",

//Supported Databases
Expand Down
2 changes: 1 addition & 1 deletion modules/dbsupport/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ dependencies {
api libraries.hibernate_core
api libraries.hibernate_envers
api libraries.hibernate_c3p0
api libraries.hibernate_ant
api libraries.c3p0 // force c3p0 version (temporary fix un Hibernate bumps up version)
api libraries.hibernate_ehcache
api libraries.jta
api libraries.jacksonDatabind
}
Expand Down
58 changes: 30 additions & 28 deletions modules/dbsupport/src/main/java/org/jpos/ee/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

package org.jpos.ee;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.hibernate.*;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
Expand All @@ -37,6 +33,10 @@
import org.hibernate.stat.SessionStatistics;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jpos.core.ConfigurationException;
import org.jpos.core.Environment;
import org.jpos.ee.support.ModuleUtils;
Expand Down Expand Up @@ -152,7 +152,7 @@ public SessionFactory getSessionFactory() {
if (sf instanceof SessionFactoryImpl) {
dialect = ((SessionFactoryImpl) sf).getJdbcServices().getDialect();
}
} catch (IOException | ConfigurationException | DocumentException | InterruptedException e) {
} catch (IOException | ConfigurationException | InterruptedException | JDOMException e) {
throw new RuntimeException("Could not configure session factory", e);
} finally {
sfSem.release();
Expand All @@ -178,7 +178,7 @@ public static synchronized void invalidateSessionFactories() {
sessionFactories.clear();
}

private SessionFactory newSessionFactory() throws IOException, ConfigurationException, DocumentException, InterruptedException {
private SessionFactory newSessionFactory() throws IOException, ConfigurationException, InterruptedException, JDOMException {
Metadata md = getMetadata();
try {
newSessionSem.acquireUninterruptibly();
Expand All @@ -204,28 +204,26 @@ private void configureMappings(Configuration cfg) throws ConfigurationException,
for (String moduleConfig : moduleConfigs) {
addMappings(cfg, moduleConfig);
}
} catch (DocumentException e) {
} catch (JDOMException e) {
throw new ConfigurationException("Could not parse mappings document", e);
}
}

private void addMappings(Configuration cfg, String moduleConfig) throws ConfigurationException, DocumentException
{
private void addMappings(Configuration cfg, String moduleConfig) throws ConfigurationException, IOException, JDOMException {
Element module = readMappingElements(moduleConfig);
if (module != null)
{
for (Iterator l = module.elementIterator("mapping"); l.hasNext(); )
for (Element mapping: module.getChildren("mapping"))
{
Element mapping = (Element) l.next();
parseMapping(cfg, mapping, moduleConfig);
}
}
}

private void parseMapping(Configuration cfg, Element mapping, String moduleName) throws ConfigurationException
{
final String resource = mapping.attributeValue("resource");
final String clazz = mapping.attributeValue("class");
final String resource = mapping.getAttributeValue("resource");
final String clazz = mapping.getAttributeValue("class");

if (resource != null)
{
Expand All @@ -248,14 +246,20 @@ else if (clazz != null)
}
}

private Element readMappingElements(String moduleConfig) throws DocumentException
{
SAXReader reader = new SAXReader();
private Element readMappingElements(String moduleConfig) throws IOException, JDOMException {
SAXBuilder builder = createSAXBuilder();

final URL url = getClass().getClassLoader().getResource(moduleConfig);
assert url != null;
final Document doc = reader.read(url);
return doc.getRootElement().element("mappings");
final Document doc = builder.build(url);
return doc.getRootElement().getChild("mappings");
}

private SAXBuilder createSAXBuilder () {
SAXBuilder builder = new SAXBuilder ();
builder.setFeature("http://xml.org/sax/features/namespaces", true);
builder.setFeature("http://apache.org/xml/features/xinclude", true);
return builder;
}

private Properties loadProperties(String filename) throws IOException {
Expand All @@ -276,7 +280,7 @@ private Properties loadProperties(String filename) throws IOException {
* @param outputFile optional output file (may be null)
* @param create true to actually issue the create statements
*/
public void createSchema(String outputFile, boolean create) throws HibernateException, DocumentException {
public void createSchema(String outputFile, boolean create) throws HibernateException, JDOMException {
try {
// SchemaExport export = new SchemaExport(getMetadata());
SchemaExport export = new SchemaExport();
Expand Down Expand Up @@ -494,13 +498,13 @@ public void printStats()
info.addMessage("==== STATISTICS ====");
SessionStatistics statistics = session().getStatistics();
info.addMessage("==== ENTITIES ====");
Set<EntityKey> entityKeys = statistics.getEntityKeys();
Set<EntityKey> entityKeys = (Set<EntityKey>) statistics.getEntityKeys();
for (EntityKey ek : entityKeys)
{
info.addMessage(String.format("[%s] %s", ek.getIdentifier(), ek.getEntityName()));
}
info.addMessage("==== COLLECTIONS ====");
Set<CollectionKey> collectionKeys = statistics.getCollectionKeys();
Set<CollectionKey> collectionKeys = (Set<CollectionKey>) statistics.getCollectionKeys();
for (CollectionKey ck : collectionKeys)
{
info.addMessage(String.format("[%s] %s", ck.getKey(), ck.getRole()));
Expand All @@ -521,7 +525,7 @@ public String toString() {
return "DB{" + (configModifier != null ? configModifier : "") + '}';
}

private Metadata getMetadata() throws IOException, ConfigurationException, DocumentException, InterruptedException {
private Metadata getMetadata() throws IOException, ConfigurationException, InterruptedException, JDOMException {
Semaphore mdSem = mdSems.get(configModifier);
if (!mdSem.tryAcquire(60, TimeUnit.SECONDS))
throw new RuntimeException ("Unable to acquire lock");
Expand Down Expand Up @@ -600,23 +604,21 @@ private Metadata getMetadata() throws IOException, ConfigurationException, Docum
return md;
}

private void addMappings(MetadataSources mds, String moduleConfig) throws ConfigurationException, DocumentException
{
private void addMappings(MetadataSources mds, String moduleConfig) throws ConfigurationException, IOException, JDOMException {
Element module = readMappingElements(moduleConfig);
if (module != null)
{
for (Iterator l = module.elementIterator("mapping"); l.hasNext(); )
for (Element mapping : module.getChildren("mapping"))
{
Element mapping = (Element) l.next();
parseMapping(mds, mapping, moduleConfig);
}
}
}

private void parseMapping (MetadataSources mds, Element mapping, String moduleName) throws ConfigurationException
{
final String resource = mapping.attributeValue("resource");
final String clazz = mapping.attributeValue("class");
final String resource = mapping.getAttributeValue("resource");
final String clazz = mapping.getAttributeValue("class");
if (resource != null)
mds.addResource(resource);
else if (clazz != null)
Expand Down
6 changes: 3 additions & 3 deletions modules/dbsupport/src/main/java/org/jpos/ee/DBFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package org.jpos.ee;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

/**
* Functional interface to create predicates for querying {@link DBManager}
Expand Down
19 changes: 9 additions & 10 deletions modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

package org.jpos.ee;

import org.hibernate.query.criteria.internal.OrderImpl;

import javax.persistence.NoResultException;
import javax.persistence.criteria.*;
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;
Expand Down Expand Up @@ -89,8 +88,10 @@ public List<T> getAll(int offset, int limit, Map<String,Boolean> orders) {
//ORDERS
if (orders != null) {
for (Map.Entry<String, Boolean> entry : orders.entrySet()) {
OrderImpl order = new OrderImpl(root.get(entry.getKey()), entry.getValue());
orderList.add(order);
if (entry.getValue() == null || entry.getValue())
orderList.add(criteriaBuilder.asc(root.get(entry.getKey())));
else
orderList.add(criteriaBuilder.desc(root.get(entry.getKey())));
}
}
Predicate[] predicates = buildFilters(root);
Expand All @@ -102,10 +103,9 @@ public List<T> getAll(int offset, int limit, Map<String,Boolean> orders) {
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 @@ -142,10 +142,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
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.io.IOException;

@Converter(autoApply = true)
public class ObjectNodeConverter implements AttributeConverter<ObjectNode,String> {
public class ObjectNodeConverter implements AttributeConverter<ObjectNode, String> {

@Override
public String convertToDatabaseColumn(ObjectNode attribute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

package org.jpos.ee.converter;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import org.jpos.util.Tags;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true)
public class TagsConverter implements AttributeConverter<Tags,String> {
public class TagsConverter implements AttributeConverter<Tags, String> {
@Override
public String convertToDatabaseColumn(Tags tags) {
return tags == null ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;

Expand All @@ -37,8 +36,7 @@
import java.sql.Types;
import java.text.SimpleDateFormat;

public class JsonType implements UserType {
private static int[] TYPES = { Types.VARCHAR };
public class JsonType implements UserType<Object> {

private static ObjectMapper mapper =
new ObjectMapper()
Expand All @@ -51,8 +49,8 @@ public class JsonType implements UserType {
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

@Override
public int[] sqlTypes() {
return TYPES;
public int getSqlType() {
return Types.VARCHAR;
}

@Override
Expand All @@ -70,20 +68,21 @@ public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}


/**
* Retrieve an instance of the mapped class from a JDBC resultset. Implementors
* should handle possibility of null values.
*
* @param rs a JDBC result set
* @param names the column names
* @param position the column position
* @param session
* @param owner the containing entity @return Object
* @throws HibernateException
* @throws SQLException
*/
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
String json = rs.getString(names[0]);
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
String json = rs.getString(position);
if (rs.wasNull())
return null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
*
* @author [email protected]
*/
public class JsonbType implements UserType {
public class JsonbType implements UserType<Object> {

private static ObjectMapper mapper =
new ObjectMapper()
Expand All @@ -56,12 +56,12 @@ public class JsonbType implements UserType {
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

@Override
public int[] sqlTypes() {
return new int[]{Types.JAVA_OBJECT};
public int getSqlType() {
return Types.OTHER;
}

@Override
public Class<ObjectNode> returnedClass() {
public Class returnedClass() {
return ObjectNode.class;
}

Expand All @@ -87,10 +87,10 @@ public int hashCode(final Object x) {

@Override
public Object nullSafeGet(ResultSet rs,
String[] names,
int position,
SharedSessionContractImplementor session,
Object owner) throws SQLException {
final String json = rs.getString(names[0]);
final String json = rs.getString(position);
if (json == null) {
return null;
}
Expand Down
Loading
Loading