Polishing.
TypedParameterValue class only gets loaded once. Renamed the method to better capture its behaviour. See #2860 See #2861
This commit is contained in:
@@ -190,8 +190,20 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor, Quer
|
||||
}
|
||||
};
|
||||
|
||||
private static final boolean hibernatePresent = ClassUtils.isPresent("org.hibernate.query.TypedParameterValue",
|
||||
PersistenceProvider.class.getClassLoader());
|
||||
private static final Class<?> typedParameterValueClass;
|
||||
|
||||
static {
|
||||
|
||||
Class<?> type;
|
||||
try {
|
||||
type = ClassUtils.forName("org.hibernate.query.TypedParameterValue",
|
||||
PersistenceProvider.class.getClassLoader());
|
||||
} catch (ClassNotFoundException e) {
|
||||
type = null;
|
||||
}
|
||||
typedParameterValueClass = type;
|
||||
}
|
||||
|
||||
private static final Collection<PersistenceProvider> ALL = List.of(HIBERNATE, ECLIPSELINK, GENERIC_JPA);
|
||||
|
||||
static ConcurrentReferenceHashMap<Class<?>, PersistenceProvider> CACHE = new ConcurrentReferenceHashMap<>();
|
||||
@@ -319,27 +331,14 @@ public enum PersistenceProvider implements QueryExtractor, ProxyIdAccessor, Quer
|
||||
* empty string for query creation.
|
||||
*
|
||||
* @param value
|
||||
* @return the original value or an empty string.
|
||||
* @return the original value or null.
|
||||
* @since 3.0
|
||||
*/
|
||||
public static Object condense(Object value) {
|
||||
public static Object unwrapTypedParameterValue(Object value) {
|
||||
|
||||
if (hibernatePresent) {
|
||||
|
||||
try {
|
||||
|
||||
Class<?> typeParameterValue = ClassUtils.forName("org.hibernate.query.TypedParameterValue",
|
||||
PersistenceProvider.class.getClassLoader());
|
||||
|
||||
if (typeParameterValue.isInstance(value)) {
|
||||
return null;
|
||||
}
|
||||
} catch (ClassNotFoundException | LinkageError o_O) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
return typedParameterValueClass != null && typedParameterValueClass.isInstance(value) //
|
||||
? null //
|
||||
: value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -233,24 +233,24 @@ class ParameterMetadataProvider {
|
||||
|
||||
Assert.notNull(value, "Value must not be null");
|
||||
|
||||
Object condensedValue = PersistenceProvider.condense(value);
|
||||
Object unwrapped = PersistenceProvider.unwrapTypedParameterValue(value);
|
||||
|
||||
if (condensedValue == null || expression.getJavaType() == null) {
|
||||
return condensedValue;
|
||||
if (unwrapped == null || expression.getJavaType() == null) {
|
||||
return unwrapped;
|
||||
}
|
||||
|
||||
if (String.class.equals(expression.getJavaType()) && !noWildcards) {
|
||||
|
||||
switch (type) {
|
||||
case STARTING_WITH:
|
||||
return String.format("%s%%", escape.escape(condensedValue.toString()));
|
||||
return String.format("%s%%", escape.escape(unwrapped.toString()));
|
||||
case ENDING_WITH:
|
||||
return String.format("%%%s", escape.escape(condensedValue.toString()));
|
||||
return String.format("%%%s", escape.escape(unwrapped.toString()));
|
||||
case CONTAINING:
|
||||
case NOT_CONTAINING:
|
||||
return String.format("%%%s%%", escape.escape(condensedValue.toString()));
|
||||
return String.format("%%%s%%", escape.escape(unwrapped.toString()));
|
||||
default:
|
||||
return condensedValue;
|
||||
return unwrapped;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -714,21 +714,21 @@ class StringQuery implements DeclaredQuery {
|
||||
@Override
|
||||
public Object prepare(@Nullable Object value) {
|
||||
|
||||
Object condensedValue = PersistenceProvider.condense(value);
|
||||
if (condensedValue == null) {
|
||||
Object unwrapped = PersistenceProvider.unwrapTypedParameterValue(value);
|
||||
if (unwrapped == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case STARTING_WITH:
|
||||
return String.format("%s%%", condensedValue);
|
||||
return String.format("%s%%", unwrapped);
|
||||
case ENDING_WITH:
|
||||
return String.format("%%%s", condensedValue);
|
||||
return String.format("%%%s", unwrapped);
|
||||
case CONTAINING:
|
||||
return String.format("%%%s%%", condensedValue);
|
||||
return String.format("%%%s%%", unwrapped);
|
||||
case LIKE:
|
||||
default:
|
||||
return condensedValue;
|
||||
return unwrapped;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user