Skip to content

Commit

Permalink
deprecate hibernate criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Sep 16, 2024
1 parent 19adb0f commit 1efb6b5
Show file tree
Hide file tree
Showing 45 changed files with 4,536 additions and 3,051 deletions.
382 changes: 382 additions & 0 deletions java/.vscode/java-formatter.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions java/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
"prettier.requireConfig": true,
"java.compile.nullAnalysis.mode": "automatic",
"java.jdt.ls.vmargs": "-XX:+UseParallelGC -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -Dsun.zip.disableMemoryMapping=true -Xmx2G -Xms100m -Xlog:disable",
"java.format.settings.url": "https://raw.githubusercontent.com/uyuni-project/uyuni/master/java/conf/eclipse/code_formatter_rhn.xml",
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public class CachedStatement implements Serializable {
* @param parsedQuery This immutable query definition.
* @param orig The parent query.
*/
private CachedStatement(Session sessionIn, String newName, ParsedQuery parsedQuery, List<String> paramsIn,
CachedStatement orig) {
private CachedStatement(Session sessionIn, String newName, ParsedQuery parsedQuery,
List<String> paramsIn, CachedStatement orig) {
this(sessionIn, parsedQuery);
parentStatement = orig;
this.name = newName;
Expand Down Expand Up @@ -211,10 +211,9 @@ public void modifyQuery(String replaceToken, List<String> valueList,
firstValue = false;
}
if (querySanitizer != null && !querySanitizer.isSanitary(value)) {
throw new IllegalArgumentException(
"Attempt to modify query for token '" + replaceToken +
"' with value that did not pass sanitary check. Value: " +
value);
throw new IllegalArgumentException("Attempt to modify query for token '" +
replaceToken +
"' with value that did not pass sanitary check. Value: " + value);
}
sb.append("'").append(value).append("'");
}
Expand Down Expand Up @@ -255,8 +254,7 @@ List<Integer> executeUpdates(List<Map<String, Object>> parameterList) {
throw SqlExceptionTranslator.sqlException(e);
}
catch (HibernateException he) {
throw new
HibernateRuntimeException(
throw new HibernateRuntimeException(
"HibernateException executing CachedStatement", he);

}
Expand All @@ -267,7 +265,6 @@ List<Integer> executeUpdates(List<Map<String, Object>> parameterList) {
});
}


<T> DataResult<T> execute(Map<String, ?> parameters, Mode mode) {
return internalExecute(parameters, null, mode);
}
Expand All @@ -276,8 +273,7 @@ <T> DataResult<T> execute(List<?> parameters, Mode mode) {
return internalExecute(null, parameters, mode);
}

<T> DataResult<T> execute(Map<String, ?> parameters, List<?> inClause,
Mode mode) {
<T> DataResult<T> execute(Map<String, ?> parameters, List<?> inClause, Mode mode) {
return internalExecute(parameters, inClause, mode);
}

Expand All @@ -289,18 +285,21 @@ private Integer internalExecuteUpdateNoSubClause(Map<String, ?> parameters, Mode
return 0;
}

private <T> DataResult<T> internalExecuteNoSubClause(Map<String, ?> parameters, Mode mode) {
private <T> DataResult<T> internalExecuteNoSubClause(Map<String, ?> parameters,
Mode mode) {
storeForRestart(parameters, null, mode);
this.sqlStatement = NamedPreparedStatement.replaceBindParams(sqlStatement, qMap);
Object resultObj = executeChecking(sqlStatement, qMap, parameters, mode, null);
Class<DataResult<T>> drClazz = (Class<DataResult<T>>)(Class<?>) DataResult.class;
Class<DataResult<T>> drClazz = (Class<DataResult<T>>) (Class<?>) DataResult.class;
if (drClazz.isAssignableFrom(resultObj.getClass())) {
return drClazz.cast(executeChecking(sqlStatement, qMap, parameters, mode, null));
return drClazz
.cast(executeChecking(sqlStatement, qMap, parameters, mode, null));
}
return new DataResult<>(mode);
}

private int internalExecuteUpdate(Map<String, ?> parameters, List<?> inClause, Mode mode) {
private int internalExecuteUpdate(Map<String, ?> parameters, List<?> inClause,
Mode mode) {
storeForRestart(parameters, inClause, mode);
this.sqlStatement = NamedPreparedStatement.replaceBindParams(sqlStatement, qMap);

Expand All @@ -309,11 +308,15 @@ private int internalExecuteUpdate(Map<String, ?> parameters, List<?> inClause, M

int subStart = 0;
while (subStart < inClause.size()) {
int subLength = subStart + BATCH_SIZE >= inClause.size() ? inClause.size() - subStart : BATCH_SIZE;
int subLength = subStart + BATCH_SIZE >= inClause.size() ?
inClause.size() - subStart :
BATCH_SIZE;

List<?> subClause = inClause.subList(subStart, subStart + subLength);
String finalQuery = sqlStatement.replace("%s", commaSeparatedList(subClause));
Object resultObj = executeChecking(finalQuery, qMap, parameters, mode, null);
String finalQuery =
sqlStatement.replace("%s", commaSeparatedList(subClause));
Object resultObj =
executeChecking(finalQuery, qMap, parameters, mode, null);
subStart += subLength;

if (resultObj instanceof Integer) {
Expand All @@ -332,8 +335,9 @@ private List<String> queryBatches(List<?> inClause) {

int subStart = 0;
while (subStart < inClause.size()) {
int subLength = subStart + BATCH_SIZE >= inClause.size() ?
inClause.size() - subStart : BATCH_SIZE;
int subLength =
subStart + BATCH_SIZE >= inClause.size() ? inClause.size() - subStart :
BATCH_SIZE;

List<?> subClause = inClause.subList(subStart, subStart + subLength);
queries.add(sqlStatement.replace("%s", commaSeparatedList(subClause)));
Expand All @@ -343,7 +347,8 @@ private List<String> queryBatches(List<?> inClause) {
}

@SuppressWarnings("unchecked")
private <T> DataResult<T> internalExecute(Map<String, ?> parameters, List<?> inClause, Mode mode) {
private <T> DataResult<T> internalExecute(Map<String, ?> parameters, List<?> inClause,
Mode mode) {

storeForRestart(parameters, inClause, mode);
this.sqlStatement = NamedPreparedStatement.replaceBindParams(sqlStatement, qMap);
Expand All @@ -352,14 +357,15 @@ private <T> DataResult<T> internalExecute(Map<String, ?> parameters, List<?> inC
if (inClause == null || inClause.isEmpty()) {
return new DataResult<>(mode);
}
Class<DataResult<T>> drClazz = (Class<DataResult<T>>)(Class<?>) DataResult.class;
Class<DataResult<T>> drClazz =
(Class<DataResult<T>>) (Class<?>) DataResult.class;

List<String> batches = queryBatches(inClause);
List<DataResult<T>> results = batches.stream()
.map(finalQuery -> executeChecking(finalQuery, qMap, parameters, mode, null))
.map(finalQuery -> executeChecking(finalQuery, qMap, parameters, mode,
null))
.filter(res -> drClazz.isAssignableFrom(res.getClass()))
.map(res -> drClazz.cast(res))
.collect(Collectors.toList());
.map(res -> drClazz.cast(res)).collect(Collectors.toList());
DataResult<T> result = null;
if (!results.isEmpty()) {
result = results.get(0);
Expand Down Expand Up @@ -410,7 +416,7 @@ Collection<Object> executeElaborator(List<Object> resultList, Mode mode,

@SuppressWarnings("unchecked")
private Collection<Object> executeElaboratorBatch(List<Object> resultList, Mode mode,
Map<String, ?> parametersIn) {
Map<String, ?> parametersIn) {

this.sqlStatement = NamedPreparedStatement.replaceBindParams(sqlStatement, qMap);

Expand Down Expand Up @@ -451,7 +457,8 @@ private Collection<Object> executeElaboratorBatch(List<Object> resultList, Mode
if (!getName().equals("")) {
newName = getName() + len;
}
CachedStatement cs = new CachedStatement(session, newName, protoQuery, newParams, this);
CachedStatement cs =
new CachedStatement(session, newName, protoQuery, newParams, this);
cs.modifyQuery("%s", bindParams.toString());
return cs.executeElaboratorBatch(resultList, mode, parameters);
}
Expand Down Expand Up @@ -482,8 +489,7 @@ private Collection<Object> executeElaboratorBatch(List<Object> resultList, Mode
* @param mode Mode for selection queries
* @param dr Data result list or null
* @return either an integer count of the number of rows updated, or the
* DataResult. Casting to int or DataResult is caller's
* responsibility
* DataResult. Casting to int or DataResult is caller's responsibility
*/
private Object executeChecking(String sql, Map<String, List<Integer>> parameterMap,
Map<String, ?> parameters, Mode mode, List<Object> dr) {
Expand Down Expand Up @@ -516,8 +522,7 @@ private Object executeChecking(String sql, Map<String, List<Integer>> parameterM
* @param mode Mode for selection queries
* @param dr Data result list or null
* @return either an integer count of the number of rows updated, or the
* DataResult. Casting to int or DataResult is caller's
* responsibility
* DataResult. Casting to int or DataResult is caller's responsibility
* @throws SQLException
*/
private Object execute(Connection connection, String sql,
Expand Down Expand Up @@ -857,11 +862,11 @@ private Object getObject(ResultSet rs, String columnName) throws SQLException {
// vs the real date: August 23, 2005 1:36:12 PM PDT
if (columnValue instanceof Date ||
("oracle.sql.TIMESTAMPLTZ"
.equals(columnValue.getClass().getCanonicalName())) ||
.equals(columnValue.getClass().getCanonicalName())) ||
("oracle.sql.TIMESTAMP"
.equals(columnValue.getClass().getCanonicalName())) ||
.equals(columnValue.getClass().getCanonicalName())) ||
("oracle.sql.TIMESTAMPTZ"
.equals(columnValue.getClass().getCanonicalName()))) {
.equals(columnValue.getClass().getCanonicalName()))) {
return rs.getTimestamp(columnName);
}
else if (columnValue instanceof BigDecimal) {
Expand Down Expand Up @@ -956,17 +961,18 @@ public <T> DataResult<T> restartQuery(Session newSession) {
session = newSession;

return restartData == null ? null :
internalExecute(restartData.getParameters(),
restartData.getInClause(), restartData.getMode());
internalExecute(restartData.getParameters(), restartData.getInClause(),
restartData.getMode());
}

/**
* Executes multiple updates with one only prepared statement in batch mode.
*
* @param batch a list of parameter maps
* @return an array of update counts containing one element for each command in the batch
* @return an array of update counts containing one element for each command
* in the batch
*/
public int [] executeBatchUpdates(DataResult<Map<String, Object>> batch) {
public int[] executeBatchUpdates(DataResult<Map<String, Object>> batch) {
return doWithStolenConnection(connection -> {
try {
sqlStatement = NamedPreparedStatement.replaceBindParams(sqlStatement, qMap);
Expand All @@ -977,8 +983,7 @@ public <T> DataResult<T> restartQuery(Session newSession) {
throw SqlExceptionTranslator.sqlException(e);
}
catch (HibernateException he) {
throw new
HibernateRuntimeException(
throw new HibernateRuntimeException(
"HibernateException executing CachedStatement", he);

}
Expand All @@ -989,8 +994,9 @@ public <T> DataResult<T> restartQuery(Session newSession) {
});
}

private int [] executeBatch(Connection connection, String sql,
Map<String, List<Integer>> parameterMap, DataResult<Map<String, Object>> batch) throws SQLException {
private int[] executeBatch(Connection connection, String sql,
Map<String, List<Integer>> parameterMap, DataResult<Map<String, Object>> batch)
throws SQLException {
if (log.isDebugEnabled()) {
log.debug("execute() - Executing: {}", sql);
log.debug("execute() - With: {}", batch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import io.prometheus.client.hibernate.HibernateStatisticsCollector;


/**
* Manages the lifecycle of Hibernate SessionFactory and associated
* thread-scoped Hibernate sessions.
Expand All @@ -54,11 +53,11 @@ abstract class AbstractConnectionManager implements ConnectionManager {
private final Set<String> packageNames;
private String unitLabelValue;


/**
* Set up the connection manager.
*
* @param packageNamesSet set of packages that will be scanned for hbm.xml files on initialization.
* @param packageNamesSet set of packages that will be scanned for hbm.xml
* files on initialization.
*/
protected AbstractConnectionManager(Set<String> packageNamesSet) {
this.LOG = LogManager.getLogger(getClass());
Expand Down Expand Up @@ -189,19 +188,20 @@ protected void createSessionFactory() {
config.addProperties(getConfigurationProperties());

// Collect all the hbm files available in the specified packages
packageNames.stream()
.map(FinderFactory::getFinder)
.flatMap(finder -> finder.find("hbm.xml").stream())
.peek(hbmFile -> LOG.debug("Adding resource {}", hbmFile))
.forEach(config::addResource);
packageNames.stream().map(FinderFactory::getFinder)
.flatMap(finder -> finder.find("hbm.xml").stream())
.peek(hbmFile -> LOG.debug("Adding resource {}", hbmFile))
.forEach(config::addResource);

// Invoke each configurator to add additional entries to Hibernate config
// Invoke each configurator to add additional entries to Hibernate
// config
configurators.forEach(configurator -> configurator.addConfig(config));

// TODO: Fix auto-discovery (see commit: e92b062)
getAnnotatedClasses().forEach(config::addAnnotatedClass);

// add empty varchar interceptor to automatically convert empty to null
// add empty varchar interceptor to automatically convert empty to
// null
config.setInterceptor(new EmptyVarcharInterceptor(true));

sessionFactory = config.buildSessionFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ class DefaultConnectionManager extends AbstractConnectionManager {
@Override
protected Properties getConfigurationProperties() {
final Properties hibProperties = Config.get().getNamespaceProperties("hibernate");
hibProperties.put("hibernate.connection.username", Config.get().getString(ConfigDefaults.DB_USER));
hibProperties.put("hibernate.connection.password", Config.get().getString(ConfigDefaults.DB_PASSWORD));
hibProperties.put("hibernate.connection.url", ConfigDefaults.get().getJdbcConnectionString());
hibProperties.put("hibernate.connection.username",
Config.get().getString(ConfigDefaults.DB_USER));
hibProperties.put("hibernate.connection.password",
Config.get().getString(ConfigDefaults.DB_PASSWORD));
hibProperties.put("hibernate.connection.url",
ConfigDefaults.get().getJdbcConnectionString());
return hibProperties;
}

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

import com.redhat.rhn.common.db.DatabaseException;


/**
* DuplicateObjectException
*/
Expand All @@ -38,10 +37,9 @@ public DuplicateObjectException(String message) {
/**
* Constructor
* @param message exception message
* @param cause the cause (which is saved for later retrieval
* by the Throwable.getCause() method). (A null value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @param cause the cause (which is saved for later retrieval by the
* Throwable.getCause() method). (A null value is permitted, and indicates
* that the cause is nonexistent or unknown.)
*/
public DuplicateObjectException(String message, Throwable cause) {
super(message, cause);
Expand Down
Loading

0 comments on commit 1efb6b5

Please sign in to comment.