All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.kivu4j.utils.hibernate.ExampleParameterExecutorCallback 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 net.sourceforge.kivu4j.utils.api.repository.ExampleParameter;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.springframework.orm.hibernate3.HibernateCallback;

/**
 * Example查询回调类
 *
 * @author Lucky Yang
 */
public class ExampleParameterExecutorCallback 
	implements HibernateCallback> {
	
	private ExampleParameter parameter;
	private Class clazz;
	
	public ExampleParameterExecutorCallback(Class clazz, ExampleParameter parameter) {
		super();
		this.clazz = clazz;
		this.parameter = parameter;
	}
    
	@SuppressWarnings("unchecked")
	@Override
	public Collection doInHibernate(Session session) 
		throws HibernateException, SQLException {
		
		Example e = Example.create(this.parameter.getExample()).enableLike(MatchMode.ANYWHERE);
		Criteria criteria = session.createCriteria(this.clazz);
		criteria.add(e);
        
        if (this.parameter.isUnique()){
        	criteria.setMaxResults(1);	
        }else if (this.parameter.getLimit() > 0){
        	criteria.setFirstResult(this.parameter.getStart());
        	criteria.setMaxResults(this.parameter.getLimit() + 1);        	
        }
        
        if (this.parameter.isDistanct())
        	criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return criteria.list();  
	}
		
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy