Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.appform.dropwizard.sharding</groupId>
<artifactId>db-sharding-bundle</artifactId>
<version>2.1.12-1</version>
<version>2.1.12-HIBERNATE6-RC0</version>
<name>Dropwizard Database Sharding Bundle</name>
<url>https://github.com/santanusinha/dropwizard-db-sharding-bundle</url>
<description>Application layer database sharding over SQL dbs</description>
Expand Down Expand Up @@ -80,10 +80,15 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dropwizard.version>2.1.12</dropwizard.version>

<hibernate.version>6.6.0.Final</hibernate.version>
<hibernate-validator.version>6.2.5.Final</hibernate-validator.version>
<jackson-datatype-hibernate6.version>2.16.1</jackson-datatype-hibernate6.version>
<jasypt-hibernate6.version>1.9.6</jasypt-hibernate6.version>

<lombok.version>1.18.42</lombok.version>
<cglib.version>3.3.0</cglib.version>
<caffeine.version>2.9.3</caffeine.version>
<jasypt-hibernate5.version>1.9.5</jasypt-hibernate5.version>
<junit.jupiter.version>5.9.3</junit.jupiter.version>
<h2.version>2.2.224</h2.version>
<mockito.version>4.3.1</mockito.version>
Expand All @@ -98,14 +103,6 @@
<scope>import</scope>
<version>${dropwizard.version}</version>
</dependency>
<!-- Dropwizard Dependencies for transitive deps like hibernate-core -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-dependencies</artifactId>
<type>pom</type>
<scope>import</scope>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
Expand Down Expand Up @@ -148,23 +145,29 @@
<artifactId>dropwizard-db</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
</exclusion>
</exclusions>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate6</artifactId>
<version>${jackson-datatype-hibernate6.version}</version>
</dependency>
<dependency>
<groupId>com.github.phaneesh</groupId>
<artifactId>jasypt-hibernate5</artifactId>
<version>${jasypt-hibernate5.version}</version>
<artifactId>jasypt-hibernate6</artifactId>
<version>${jasypt-hibernate6.version}</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -192,6 +195,10 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.persistence.Entity;

import jakarta.persistence.Entity;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ClassUtils;
import org.jasypt.encryption.pbe.StandardPBEBigDecimalEncryptor;
import org.jasypt.encryption.pbe.StandardPBEBigIntegerEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.hibernate5.encryptor.HibernatePBEEncryptorRegistry;
import org.jasypt.hibernate6.encryptor.HibernatePBEEncryptorRegistry;
import org.jasypt.hibernate6.encryptor.converters.AttributeEncryptionProvider;
import org.jasypt.iv.StringFixedIvGenerator;
import org.reflections.Reflections;

Expand Down Expand Up @@ -143,8 +145,8 @@ protected void registerStringEncryptor(String tenantId, ShardingBundleOptions sh
encryptorRegistry.registerPBEStringEncryptor(tenantId, "encryptedCalendarAsString",
strongEncryptor);
} else {
encryptorRegistry.registerPBEStringEncryptor("encryptedString", strongEncryptor);
encryptorRegistry.registerPBEStringEncryptor("encryptedCalendarAsString", strongEncryptor);
encryptorRegistry.registerPBEStringEncryptor(AttributeEncryptionProvider.STRING_ENCRYPTER_NAME, strongEncryptor);
encryptorRegistry.registerPBEStringEncryptor(AttributeEncryptionProvider.CALENDER_ENCRYPTER_NAME, strongEncryptor);
}
}

Expand All @@ -160,7 +162,7 @@ protected void registerBigIntegerEncryptor(String tenantId,
encryptorRegistry.registerPBEBigIntegerEncryptor(tenantId, "encryptedBigInteger",
strongEncryptor);
} else {
encryptorRegistry.registerPBEBigIntegerEncryptor("encryptedBigInteger",
encryptorRegistry.registerPBEBigIntegerEncryptor(AttributeEncryptionProvider.BIG_INTEGER_ENCRYPTER_NAME,
strongEncryptor);
}
}
Expand All @@ -177,7 +179,7 @@ protected void registerBigDecimalEncryptor(String tenantId,
encryptorRegistry.registerPBEBigDecimalEncryptor(tenantId, "encryptedBigDecimal",
strongEncryptor);
} else {
encryptorRegistry.registerPBEBigDecimalEncryptor("encryptedBigDecimal",
encryptorRegistry.registerPBEBigDecimalEncryptor(AttributeEncryptionProvider.BIG_DECIMAL_ENCRYPTER_NAME,
strongEncryptor);
}
}
Expand All @@ -192,7 +194,7 @@ protected void registerByteEncryptor(String tenantId, ShardingBundleOptions shar
if (Objects.nonNull(tenantId)) {
encryptorRegistry.registerPBEByteEncryptor(tenantId, "encryptedBinary", strongEncryptor);
} else {
encryptorRegistry.registerPBEByteEncryptor("encryptedBinary", strongEncryptor);
encryptorRegistry.registerPBEByteEncryptor(AttributeEncryptionProvider.BYTE_ENCRYPTER_NAME, strongEncryptor);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package io.appform.dropwizard.sharding;

import com.codahale.metrics.MetricRegistry;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module.Feature;
import com.fasterxml.jackson.datatype.hibernate6.Hibernate6Module;
import com.fasterxml.jackson.datatype.hibernate6.Hibernate6Module.Feature;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -192,7 +192,7 @@ public void initialize(Bootstrap<?> bootstrap) {
// Registers the Hibernate5Module with Jackson's ObjectMapper to support serialization of Hibernate entities.
// Enables FORCE_LAZY_LOADING to automatically fetch and serialize lazy-loaded associations (e.g., @OneToMany(fetch = LAZY))
// during JSON serialization, as long as the Hibernate session is still open.
bootstrap.getObjectMapper().registerModule(new Hibernate5Module().enable(Feature.FORCE_LAZY_LOADING));
bootstrap.getObjectMapper().registerModule(new Hibernate6Module().enable(Feature.FORCE_LAZY_LOADING));
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package io.appform.dropwizard.sharding.caching;


import org.hibernate.criterion.DetachedCriteria;

import java.util.List;
import java.util.function.Function;

Expand All @@ -38,15 +36,15 @@ public interface RelationalCache<V> {
void put(String parentKey, Object key, V entity);

/**
* Write through method that will be called if cache enabled {@link io.appform.dropwizard.sharding.dao.CacheableRelationalDao#select(String, DetachedCriteria, int, int, Function)} is used
* Write through method that will be called if cache enabled {@link io.appform.dropwizard.sharding.dao.CacheableRelationalDao#select(String, io.appform.dropwizard.sharding.query.QuerySpec, int, int, Function)} is used
*
* @param parentKey The key of the parent that the entity is related to
* @param entities List of entities that needs to be written into cache
*/
void put(String parentKey, List<V> entities);

/**
* Write through method that will be called if cache enabled {@link io.appform.dropwizard.sharding.dao.CacheableRelationalDao#select(String, DetachedCriteria, int, int, Function)} is used
* Write through method that will be called if cache enabled {@link io.appform.dropwizard.sharding.dao.CacheableRelationalDao#select(String, io.appform.dropwizard.sharding.query.QuerySpec, int, int, Function)} is used
*
* @param parentKey The key of the parent that the entity is related to
* @param entities List of entities that needs to be written into cache
Expand All @@ -72,15 +70,15 @@ public interface RelationalCache<V> {
V get(String parentKey, Object key);

/**
* Read through method that will be called if a cache enabled {@link io.appform.dropwizard.sharding.dao.CacheableRelationalDao#select(String, DetachedCriteria, int, int, Function)} is used
* Read through method that will be called if a cache enabled {@link io.appform.dropwizard.sharding.dao.CacheableRelationalDao#select(String, io.appform.dropwizard.sharding.query.QuerySpec, int, int, Function)} is used
*
* @param parentKey The key of the parent the entity is related to
* @return Entities that was read through the cache
*/
List<V> select(String parentKey);

/**
* Read through method that will be called if a cache enabled {@link io.appform.dropwizard.sharding.dao.CacheableRelationalDao#select(String, DetachedCriteria, int, int, Function)} is used
* Read through method that will be called if a cache enabled {@link io.appform.dropwizard.sharding.dao.CacheableRelationalDao#select(String, io.appform.dropwizard.sharding.query.QuerySpec, int, int, Function)} is used
*
* @param parentKey The key of the parent the entity is related to
* @return Entities that was read through the cache
Expand Down
58 changes: 15 additions & 43 deletions src/main/java/io/appform/dropwizard/sharding/dao/AbstractDAO.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package io.appform.dropwizard.sharding.dao;

import io.dropwizard.util.Generics;
import org.hibernate.Criteria;
import jakarta.persistence.criteria.CriteriaQuery;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.NonUniqueResultException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.hibernate.query.internal.AbstractProducedQuery;

import javax.persistence.criteria.CriteriaQuery;
import java.io.Serializable;
import java.util.List;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -43,18 +42,6 @@ protected Session currentSession() {
return sessionFactory.getCurrentSession();
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class was copied from dropwizard hibernate package into this repo. Let's check dropwizard-hibernate module for 5.x and see if anything else should be added/removed from here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No major changes apart from import changes + movement from Serilizable to Object for id lookup

/**
* Creates a new {@link Criteria} query for {@code <E>}.
*
* @return a new {@link Criteria} query
* @see Session#createCriteria(Class)
* @deprecated Use {@link AbstractDAO#criteriaQuery()} instead.
*/
@Deprecated
protected Criteria criteria() {
return currentSession().createCriteria(entityClass);
}

/**
* Creates a new {@link CriteriaQuery} for {@code <E>}.
*
Expand Down Expand Up @@ -116,25 +103,22 @@ public Class<E> getEntityClass() {
* @throws HibernateException if there is more than one matching result
*/
protected E uniqueResult(CriteriaQuery<E> criteriaQuery) throws HibernateException {
return AbstractProducedQuery.uniqueElement(
return uniqueElement(
currentSession()
.createQuery(requireNonNull(criteriaQuery))
.getResultList()
);
}

/**
* Convenience method to return a single instance that matches the criteria, or null if the
* criteria returns no results.
*
* @param criteria the {@link Criteria} query to run
* @return the single result or {@code null}
* @throws HibernateException if there is more than one matching result
* @see Criteria#uniqueResult()
*/
@SuppressWarnings("unchecked")
protected E uniqueResult(Criteria criteria) throws HibernateException {
return (E) requireNonNull(criteria).uniqueResult();
private static <T> @Nullable T uniqueElement(List<T> list) throws NonUniqueResultException {
if (list.isEmpty()) {
return null;
}
final T head = list.get(0);
if (list.stream().anyMatch(element -> element != head)) {
throw new NonUniqueResultException(list.size());
}
return head;
}

/**
Expand All @@ -150,18 +134,6 @@ protected E uniqueResult(Query<E> query) throws HibernateException {
return requireNonNull(query).uniqueResult();
}

/**
* Get the results of a {@link Criteria} query.
*
* @param criteria the {@link Criteria} query to run
* @return the list of matched query results
* @see Criteria#list()
*/
@SuppressWarnings("unchecked")
protected List<E> list(Criteria criteria) throws HibernateException {
return requireNonNull(criteria).list();
}

/**
* Get the results of a {@link CriteriaQuery} query.
*
Expand Down Expand Up @@ -191,10 +163,10 @@ protected List<E> list(Query<E> query) throws HibernateException {
* @param id an identifier
* @return a persistent instance or {@code null}
* @throws HibernateException
* @see Session#get(Class, Serializable)
* @see Session#get(Class, Object)
*/
@SuppressWarnings("unchecked")
protected E get(Serializable id) {
protected E get(Object id) {
return (E) currentSession().get(entityClass, requireNonNull(id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.appform.dropwizard.sharding.dao;

import org.hibernate.criterion.DetachedCriteria;
import io.appform.dropwizard.sharding.query.QuerySpec;

import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -71,8 +71,7 @@ public Optional<T> save(String parentKey, T entity) throws Exception {
}

@Override
public List<T> select(String parentKey, DetachedCriteria criteria, int first, int numResults) throws Exception {
return delegate.select(dbNamespace, parentKey, criteria, first, numResults);
public List<T> select(String parentKey, QuerySpec<T, T> querySpec, int first, int numResults) throws Exception {
return delegate.select(dbNamespace, parentKey, querySpec, first, numResults);
}

}
Loading