Skip to content

Commit

Permalink
deprecate hibernate criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Aug 23, 2024
1 parent ee89a9c commit 05651a3
Show file tree
Hide file tree
Showing 44 changed files with 4,478 additions and 3,006 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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,27 @@ public EmptyVarcharInterceptor() {
}

/**
* Build a new instance specifying if empty varchar will be automatically converted to null.
* Build a new instance specifying if empty varchar will be automatically
* converted to null.
*
* @param autoConvertIn if true automatically convert all empty varchar fields to null.
* @param autoConvertIn if true automatically convert all empty varchar
* fields to null.
*/
public EmptyVarcharInterceptor(boolean autoConvertIn) {
this.autoConvert = autoConvertIn;
}

protected static boolean emptyStringToNull(Object entity, Serializable id,
Object[] state, String[] propertyNames, Type[] types,
boolean autoConvert) {
Object[] state, String[] propertyNames, Type[] types, boolean autoConvert) {

boolean modified = false;

for (int i = 0; i < types.length; i++) {
// type is string (VARCHAR) and state is empty string
if ((types[i] instanceof StringType) && "".equals(state[i])) {
if (LOG.isDebugEnabled()) {
LOG.debug("Object {} is setting empty string {}", entity.getClass().getCanonicalName(),
propertyNames[i]);
LOG.debug("Object {} is setting empty string {}",
entity.getClass().getCanonicalName(), propertyNames[i]);
}
if (autoConvert) {
state[i] = null;
Expand All @@ -79,23 +80,22 @@ protected static boolean emptyStringToNull(Object entity, Serializable id,
@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
return emptyStringToNull(entity, id, state, propertyNames, types,
autoConvert);
return emptyStringToNull(entity, id, state, propertyNames, types, autoConvert);
}

/**
* {@inheritDoc}
*/
@Override
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) {
return emptyStringToNull(entity, id, currentState, propertyNames,
types, autoConvert);
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
Object[] previousState, String[] propertyNames, Type[] types) {
return emptyStringToNull(entity, id, currentState, propertyNames, types,
autoConvert);
}

/**
* Flag indicating if the interceptor correct the varchar errors automatically
* Flag indicating if the interceptor correct the varchar errors
* automatically
*
* @return boolean
*/
Expand All @@ -104,7 +104,8 @@ public boolean isAutoConvert() {
}

/**
* Flag indicating if the interceptor correct the varchar errors automatically
* Flag indicating if the interceptor correct the varchar errors
* automatically
*
* @param autoConvertIn true - convert automatically
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
import java.util.Map;

/**
* This is special user collection type that should be used when
* one wants to force the recreation of a list
* on a Many-Many mapping table. The example case where
* we are using this
* This is special user collection type that should be used when one wants to
* force the recreation of a list on a Many-Many mapping table. The example case
* where we are using this
* <p>
* {@literal
* Domain Model -> Many Servers (s) - Many Config Channels(cc)
Expand All @@ -43,27 +42,24 @@
* When saving server, with hibernates default mechanism,
* the links between the Server & ConfigChannels are not deleted
* and remapped from scratch. Instead an "optimization" is done
* using updates.
* For eg, if SCC read -> (sid, ccid, position) ->{(2,1,0),(2,2,1),(2,3,2)}
* And we remove the link for CCID = 1 to end up with ->{(2,2,1),(2,3,2)}
* Hibernate achieves this in the following order
* a) remove(2,3,2) ,b) moves (2,1,0)-> (2,2,0), c) move (2,2,1) -> (2,3,1)
* Problem here is since (sid, ccid) combination must be unique
* step b fails (since 2,2,1 still exists).. So we have to force hibernate
* to do the following a) remove (2,*,*) b) insert(2,2,0) and c)insert(2,3,1)
* This list type will help us achieve that.
* }
* using updates. For eg, if SCC read -> (sid, ccid, position)
* ->{(2,1,0),(2,2,1),(2,3,2)} And we remove the link for CCID = 1 to end up
* with ->{(2,2,1),(2,3,2)} Hibernate achieves this in the following order a)
* remove(2,3,2) ,b) moves (2,1,0)-> (2,2,0), c) move (2,2,1) -> (2,3,1) Problem
* here is since (sid, ccid) combination must be unique step b fails (since
* 2,2,1 still exists).. So we have to force hibernate to do the following a)
* remove (2,*,*) b) insert(2,2,0) and c)insert(2,3,1) This list type will help
* us achieve that. }
* <p>
* {@literal
* When you use a list in hbm.xml , to use this collection
* you must specify <list name="...."
* collection-type="com.redhat.rhn.common.hibernate.ForceRecreationListType">
* }
* <p>
* Note in the above example if (sid, ccid, position) combination as
* a whole was unique we wouldn't have had to deal with this......
* but positions can be null twice for the same server, so we
* cannot enforce that constraint..
* Note in the above example if (sid, ccid, position) combination as a whole was
* unique we wouldn't have had to deal with this...... but positions can be null
* twice for the same server, so we cannot enforce that constraint..
*/
public class ForceRecreationListType implements UserCollectionType {

Expand All @@ -72,7 +68,8 @@ public class ForceRecreationListType implements UserCollectionType {
*/
@Override
public PersistentCollection instantiate(SharedSessionContractImplementor session,
CollectionPersister persister) throws HibernateException {
CollectionPersister persister)
throws HibernateException {
return new ForceRecreationList(session);
}

Expand Down Expand Up @@ -124,18 +121,19 @@ public Object indexOf(Object collection, Object entity) {
@Override
public Object replaceElements(Object original, Object target,
CollectionPersister persister, Object owner, Map copyCache,
SharedSessionContractImplementor session) throws HibernateException {
SharedSessionContractImplementor session)
throws HibernateException {
List result = (List) target;
result.clear();
result.addAll((Collection) original);
return result;
}

/**
* Instantiates an empty list. This method will be useful
* when hibernate 3.2 is added.
* Instantiates an empty list. This method will be useful when hibernate 3.2
* is added.
* @param anticipatedSize sample size
* @return an empty list.
* @return an empty list.
*/
@Override
public Object instantiate(int anticipatedSize) {
Expand All @@ -146,8 +144,8 @@ public Object instantiate(int anticipatedSize) {
}

/**
* Returns an ArrayList. Not sure what the heck this is here for.
* TODO: WHAT IS THIS FOR? 2007-11-8 jesusr
* Returns an ArrayList. Not sure what the heck this is here for. TODO: WHAT
* IS THIS FOR? 2007-11-8 jesusr
* @return ArrayList as an Object.
*/
public Object instantiate() {
Expand Down Expand Up @@ -175,7 +173,7 @@ private static class ForceRecreationList extends PersistentList {

/**
* @param session session implementation
* @param list list to persist
* @param list list to persist
*/
ForceRecreationList(SharedSessionContractImplementor session, List list) {
super(session, list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;
import java.util.Properties;


/**
* Manages the lifecycle of Hibernate SessionFactory and associated
* thread-scoped Hibernate sessions related to the reporting database.
Expand All @@ -33,24 +32,24 @@ public class ReportDbConnectionManager extends AbstractConnectionManager {
private final String dbConnectionUrl;

/**
* Construct the default report connection manager that connects to the database specified by the configuration
* properties.
* Construct the default report connection manager that connects to the
* database specified by the configuration properties.
*/
ReportDbConnectionManager() {
this(
Config.get().getString(ConfigDefaults.REPORT_DB_USER),
Config.get().getString(ConfigDefaults.REPORT_DB_PASSWORD),
ConfigDefaults.get().getReportingJdbcConnectionString()
);
this(Config.get().getString(ConfigDefaults.REPORT_DB_USER),
Config.get().getString(ConfigDefaults.REPORT_DB_PASSWORD),
ConfigDefaults.get().getReportingJdbcConnectionString());
}

/**
* Construct a report connection manager using the specified connection parameters
* Construct a report connection manager using the specified connection
* parameters
* @param dbUserIn the database user
* @param dbPasswordIn the database password
* @param dbConnectionUrlIn the connection url to reach the database
*/
ReportDbConnectionManager(String dbUserIn, String dbPasswordIn, String dbConnectionUrlIn) {
ReportDbConnectionManager(String dbUserIn, String dbPasswordIn,
String dbConnectionUrlIn) {
super(Collections.emptySet());

dbUser = dbUserIn;
Expand All @@ -60,7 +59,8 @@ public class ReportDbConnectionManager extends AbstractConnectionManager {

@Override
protected Properties getConfigurationProperties() {
final Properties hibProperties = Config.get().getNamespaceProperties("reporting.hibernate", "hibernate");
final Properties hibProperties =
Config.get().getNamespaceProperties("reporting.hibernate", "hibernate");
hibProperties.put("hibernate.connection.username", dbUser);
hibProperties.put("hibernate.connection.password", dbPass);
hibProperties.put("hibernate.connection.url", dbConnectionUrl);
Expand Down
Loading

0 comments on commit 05651a3

Please sign in to comment.