
net.sourceforge.kivu4j.utils.hibernate.QueryParameterExecutorCallback Maven / Gradle / Ivy
The newest version!
/**
* Kivu4j. Copyright (C) 2010 . see
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
package net.sourceforge.kivu4j.utils.hibernate;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map.Entry;
import net.sourceforge.kivu4j.utils.api.repository.QueryParameter;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
/**
* Query查询回调类。支持命名查询,hsql
*
* @author Lucky Yang
*/
public class QueryParameterExecutorCallback
implements HibernateCallback> {
private QueryParameter parameter;
public QueryParameterExecutorCallback(QueryParameter parameter) {
super();
this.parameter = parameter;
}
@SuppressWarnings("unchecked")
@Override
public Collection doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = null;
if (this.parameter.isNative()){
query = session.createQuery(this.parameter.getQuery());
}else{
query = session.getNamedQuery(this.parameter.getQuery());
}
for (Entry e : this.parameter.getParameters().entrySet()) {
query.setParameter(e.getKey(), e.getValue());
}
if (this.parameter.isUnique()){
query.setMaxResults(1);
}else if (this.parameter.getLimit() > 0){
query.setFirstResult(this.parameter.getStart());
query.setMaxResults(this.parameter.getLimit() + 1);
}
if (this.parameter.isDistanct())
query.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
return query.list();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy