Ability to use Criteria with Queries
------------------------------------
Key: HHH-3638
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-3638
Project: Hibernate Core
Issue Type: New Feature
Components: core, query-criteria, query-hql, query-sql
Reporter: Adriano dos Santos Fernandes
I done it, for my needs, using internal classes. It would be very great to have this
nativelly on Hibernate.
Query createQueryByExample(Query query, Entidade entity)
{
SessionImpl session = (SessionImpl) entityManager.getDelegate();
Class<?> entityClass = entity.getClass();
CriteriaImpl criteria = (CriteriaImpl) session.createCriteria(entityClass);
CriteriaQueryTranslator criteriaQuery = new CriteriaQueryTranslator(
session.getFactory(), criteria, entityClass.getName(), "z");
Example example = Example.create(entity)
.enableLike(MatchMode.START)
.excludeZeroes();
criteria.add(example);
String newSql = "select * from (" +
((HibernateQuery) query).getHibernateQuery().getQueryString() +
") z";
Object[] params = criteriaQuery.getQueryParameters().getPositionalParameterValues();
if (params.length > 0)
{
String newWhere = example.toSqlString(criteria, criteriaQuery);
newSql += "\nwhere " + newWhere;
query = entityManager.createNativeQuery(newSql, entityClass);
int n = 0;
for (Object param : params)
query.setParameter(++n, param);
}
return query;
}
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira