I have created one class to resolve this issue. This class is reflection of SQLProjection. The whole functionality is same as
that of SQLProjection except that it provides one additional functionality.
According to current scenario we can only replace the root alias with its SQL equivalent alias but with this you can provide
the alias which you want to replace with the SQL alias and its necessary to provide both alias name and criteria both together.
public class MySQLProjection implements Projection {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The sql query string to retrieve columns. */
private final String sql;
/** The sql string to specify group by query. */
private final String groupBy;
/** The types of the columns retrieved in sql query. */
private final Type[] types;
/** The aliases of the columns retrieved in sql query. */
private String[] aliases;
/** The aliases of the columns retrieved in sql query. */
private String[] columnAliases;
/** True if group by property is there means it is sql group projection. */
private boolean grouped;
/**
The set of aliases to be replaced with their SQL aliases. Here set must
contain values as POJOAlias.property from which we get sql alias for
POJOAlias. One POJOAlias with one dot operator and property is must.
*/
private Set<String> aliasSetToReplace;
@Override
public String toSqlString(final Criteria criteria, final int loc,
final CriteriaQuery criteriaQuery) throws HibernateException {
return replaceAliasWithSQLAlias(sql, criteria, criteriaQuery);
}
@Override
public String toGroupSqlString(final Criteria criteria, final CriteriaQuery criteriaQuery)
throws HibernateException {
return replaceAliasWithSQLAlias(groupBy, criteria, criteriaQuery);
}
/**
Replace alias in the given query string with their sql aliases from
criteria query.
@param queryString
the query string
@param criteria
the criteria
@param criteriaQuery
the criteria query
@return the string
*/
private String replaceAliasWithSQLAlias(final String queryString, final Criteria criteria,
final CriteriaQuery criteriaQuery) {
String sqlString = StringHelper.replace(queryString, "{alias}",
criteriaQuery.getSQLAlias(criteria));
Iterator<String> iterator = aliasSetToReplace.iterator();
String aliasPropString;
String aliasString;
int index;
while (iterator.hasNext()) {
aliasPropString = iterator.next();
index = aliasPropString.indexOf('.');
if (index > 0) {
aliasString = aliasPropString.substring(0, index);
sqlString = StringHelper.replace(sqlString, "{" + aliasString + "}",
criteriaQuery.getSQLAlias(criteria, aliasPropString));
}
}
return sqlString;
}
@Override
public Type[] getTypes(final Criteria crit, final CriteriaQuery criteriaQuery)
throws HibernateException {
return types;
}
@Override
public String toString() {
return sql;
}
/**
Instantiates a new my sql projection.
@param sqlQuery
the sql
@param columnAliasArray
the column aliases
@param typeArray
the types
*/
public MySQLProjection(final String sqlQuery, final String[] columnAliasArray,
final Type[] typeArray) {
this(sqlQuery, null, columnAliasArray, typeArray);
}
/**
Instantiates a new my sql projection.
@param sqlQuery
the sql
@param groupBySQL
the group by
@param columnAliasArray
the column aliases
@param typeArray
the types
*/
public MySQLProjection(final String sqlQuery, final String groupBySQL,
final String[] columnAliasArray, final Type[] typeArray) {
this(sqlQuery, groupBySQL, columnAliasArray, typeArray, null);
}
/**
Instantiates a new my sql projection.
@param sqlQuery
the sql
@param groupBySQL
the group by
@param columnAliasArray
the column aliases
@param typeArray
the types
@param aliasSet
the aliases map to replace
*/
public MySQLProjection(final String sqlQuery, final String groupBySQL,
final String[] columnAliasArray, final Type[] typeArray,
final Set<String> aliasSet)
I have created one class to resolve this issue. This class is reflection of SQLProjection. The whole functionality is same as
that of SQLProjection except that it provides one additional functionality.
According to current scenario we can only replace the root alias with its SQL equivalent alias but with this you can provide
the alias which you want to replace with the SQL alias and its necessary to provide both alias name and criteria both together.
public class MySQLProjection implements Projection {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The sql query string to retrieve columns. */
private final String sql;
/** The sql string to specify group by query. */
private final String groupBy;
/** The types of the columns retrieved in sql query. */
private final Type[] types;
/** The aliases of the columns retrieved in sql query. */
private String[] aliases;
/** The aliases of the columns retrieved in sql query. */
private String[] columnAliases;
/** True if group by property is there means it is sql group projection. */
private boolean grouped;
/**
*/
private Set<String> aliasSetToReplace;
@Override
public String toSqlString(final Criteria criteria, final int loc,
final CriteriaQuery criteriaQuery) throws HibernateException { return replaceAliasWithSQLAlias(sql, criteria, criteriaQuery); }
@Override
public String toGroupSqlString(final Criteria criteria, final CriteriaQuery criteriaQuery)
throws HibernateException { return replaceAliasWithSQLAlias(groupBy, criteria, criteriaQuery); }
/**
*/
private String replaceAliasWithSQLAlias(final String queryString, final Criteria criteria,
final CriteriaQuery criteriaQuery) {
String sqlString = StringHelper.replace(queryString, "{alias}",
criteriaQuery.getSQLAlias(criteria));
Iterator<String> iterator = aliasSetToReplace.iterator();
String aliasPropString;
String aliasString;
int index;
while (iterator.hasNext()) {
aliasPropString = iterator.next();
index = aliasPropString.indexOf('.');
if (index > 0) {
aliasString = aliasPropString.substring(0, index);
sqlString = StringHelper.replace(sqlString, "{" + aliasString + "}",
criteriaQuery.getSQLAlias(criteria, aliasPropString));
}
}
return sqlString;
}
@Override
public Type[] getTypes(final Criteria crit, final CriteriaQuery criteriaQuery)
throws HibernateException { return types; }
@Override
public String toString() { return sql; }
/**
*/
public MySQLProjection(final String sqlQuery, final String[] columnAliasArray,
final Type[] typeArray) { this(sqlQuery, null, columnAliasArray, typeArray); }
/**
*/
public MySQLProjection(final String sqlQuery, final String groupBySQL,
final String[] columnAliasArray, final Type[] typeArray) { this(sqlQuery, groupBySQL, columnAliasArray, typeArray, null); }
/**
*/
public MySQLProjection(final String sqlQuery, final String groupBySQL,
final String[] columnAliasArray, final Type[] typeArray,
final Set<String> aliasSet)
@Override
public String[] getAliases() { return aliases; }
@Override
public String[] getColumnAliases(final int loc) { return columnAliases; }
@Override
public boolean isGrouped() { return grouped; }
@Override
public Type[] getTypes(final String alias, final Criteria crit,
final CriteriaQuery criteriaQuery) { return null; // unsupported }
@Override
public String[] getColumnAliases(final String alias, final int loc) { return null; // unsupported } }
}