通用 DAO 和包装器

发布于 2024-10-03 12:19:16 字数 965 浏览 2 评论 0原文

为了完成一个 uni 项目,我需要创建一个通用的 DAO。

我遵循 Manning 的《Java persistence with hibernate》一书中的通用 DAO 设计。最终采用以下方法。

findById(ID id, boolean lock)
findAll()
findByExample(T example)
makePersistent(T entity)
makeTransient(T entity)
flush()
clear()

问题是这些方法对于我的项目来说还不够,我的项目包括搜索实体、排序和分页。我知道我可以通过创建类似的方法来做到这一点。

List<T> find(DetachedCriteria dc)

问题是,这意味着我向业务层公开了一个休眠接口,但我不希望这样做。

我想创建一个名为 MyCriteria 的接口和一个名为 MyCriteriaImpl 的类,它的作用与 DetachedCriteria 相同,但允许我通过仅更改实现来更改持久性提供程序。我想这是可能的。

问题是我没有时间写这样的代码。

我想知道的是,是否有一种模式可以让我创建 MyCriteria 接口,并且在 MyCriteriaImpl 中只需调用 hibernate criteria api 的方法。

就像:

MyCriteriaImpl mci = new MyCriteriaImpl();
mci.addRestrictionLike(property, String value);

这意味着调用:

DetachedCriteria dc = new DetachedCriteria();
dc.add(Restrictions.like(property, value) 

谢谢

In order to finish a uni project I need to create a generic DAO.

I followed the generic DAO design as found in the Manning's book Java persistence with hibernate. en ended up with the following methods.

findById(ID id, boolean lock)
findAll()
findByExample(T example)
makePersistent(T entity)
makeTransient(T entity)
flush()
clear()

The problem is that these method are not enough for my project, which consist of searching entities, ordering them and paging them. I understand that I can do that by creating a method like

List<T> find(DetachedCriteria dc)

The problem is that would mean that I expose an hibernate interface to the business layer and I don't want that.

I want to create an interface say MyCriteria and a class say MyCriteriaImpl that would do the same as the DetachedCriteria but would let me change the persistence provider by changing just the implementation. I guess it's possible.

The problem is that I don't have time to write such code.

What I want to know is if there is a pattern that would let me create the MyCriteria interface, and in the MyCriteriaImpl just call methods of the hibernate criteria api.

Like :

MyCriteriaImpl mci = new MyCriteriaImpl();
mci.addRestrictionLike(property, String value);

and that would just mean a call to :

DetachedCriteria dc = new DetachedCriteria();
dc.add(Restrictions.like(property, value) 

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月松间行 2024-10-10 12:19:17

Hades 在通用 DAO 界面中提供了您所需的一切。如果您可以用它替换您自己种植的东西,或者如果您只是寻找参考,您可以浏览它的代码。它支持分页等。

Hades provides all you require in a generic DAO interface. If you can replace your home grown stuff with that or you can browse through its code if you are just looking for reference. It has support for paging etc.

迎风吟唱 2024-10-10 12:19:16

用这个吧。它具有分页以及 lucene 集成。我为我的一个项目创建了这个。确保每个 DAO 都从中扩展。取出不需要的东西。

package com.isavera.hibernate;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.criterion.Criterion;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.isavera.actions.utils.Application;

@SuppressWarnings("serial")
@Transactional
public abstract class AbstractDAOImpl<E> extends HibernateDaoSupport implements AbstractDAO<E>, ApplicationContextAware {


private static final String CLASS_NAME = AbstractDAOImpl.class.getName();

private static final Logger LOG = Logger.getLogger(CLASS_NAME);

private static ApplicationContext _applicationContext;

private final Class<? extends E> _entityClass;

/**
 * @param entityClass
 */
public AbstractDAOImpl(Class<? extends E> entityClass) {
    super();

    _entityClass = entityClass;
}


public void delete(E entity) {
    LOG.entering(CLASS_NAME, "delete", entity);

    getHibernateTemplate().delete(entity);

    LOG.exiting(CLASS_NAME, "delete");
}

public void evict(E entity) {
    LOG.entering(CLASS_NAME, "evict", entity);

    getHibernateTemplate().evict(entity);

    LOG.exiting(CLASS_NAME, "evict");
}

public void deleteAll(Collection<E> entities) {
    getHibernateTemplate().deleteAll(entities);
}

@SuppressWarnings("unchecked")
public List<E> findByNamedQuery(String queryName) {
    return getHibernateTemplate().findByNamedQuery(queryName);
}

@SuppressWarnings("unchecked")
public List<E> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value) {
    return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramName, value);
}

@SuppressWarnings("unchecked")
public List<E> findByNamedQueryAndNamedParam(String queryName, String[] paramNames, Object[] values) {
    return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramNames, values);
}

public E get(Serializable id) {
    LOG.entering(CLASS_NAME, "get", id);

    @SuppressWarnings("unchecked")
    E entity = (E) getHibernateTemplate().get(_entityClass, id);

    LOG.exiting(CLASS_NAME, "get", entity);

    return entity;
}

public List<E> get(Criterion... criterion) {
    LOG.entering(CLASS_NAME, "get", criterion);

    Criteria criteria = getSession().createCriteria(_entityClass);

    for (Criterion c : criterion) {
        criteria.add(c);
    }

    @SuppressWarnings("unchecked")
    List<E> list = new ArrayList<E>(criteria.list());

    LOG.exiting(CLASS_NAME, "get", list);

    return list;
}

public boolean isEntityAttached(E entity) {
    return getHibernateTemplate().getSessionFactory().getCurrentSession().contains(entity);
}

public E load(Serializable id) {
    LOG.entering(CLASS_NAME, "load", id);

    @SuppressWarnings("unchecked")
    E entity = (E) getHibernateTemplate().load(_entityClass, id);

    LOG.exiting(CLASS_NAME, "load", entity);

    return entity;
}

public E load(Serializable id, LockMode lockMode) {
    LOG.entering(CLASS_NAME, "load", new Object[] { id, lockMode });

    @SuppressWarnings("unchecked")
    E entity = (E) getHibernateTemplate().load(_entityClass, id, lockMode);

    LOG.exiting(CLASS_NAME, "load", entity);

    return entity;
}

public void load(E entity, Serializable id) {
    LOG.entering(CLASS_NAME, "load", new Object[] { entity, id });

    getHibernateTemplate().load(entity, id);

    LOG.exiting(CLASS_NAME, "load");
}

public void lock(E entity, LockMode lockMode) {
    LOG.entering(CLASS_NAME, "lock", new Object[] { entity, lockMode });

    getHibernateTemplate().lock(entity, lockMode);

    LOG.exiting(CLASS_NAME, "lock");
}



public void saveOrUpdateAll(Collection<E> entities) {
    getHibernateTemplate().saveOrUpdateAll(entities);
}

@SuppressWarnings("unchecked")
public E merge(E entity) {
    LOG.entering(CLASS_NAME, "merge", entity);

    E persistentEntity = (E) getHibernateTemplate().merge(entity);

    LOG.exiting(CLASS_NAME, "merge", persistentEntity);

    return persistentEntity;
}

public void refresh(E entity) {
    LOG.entering(CLASS_NAME, "refresh", entity);

    getHibernateTemplate().refresh(entity);

    LOG.exiting(CLASS_NAME, "refresh");
}

public Long save(E entity) {
    LOG.entering(CLASS_NAME, "save", entity);

    LOG.exiting(CLASS_NAME, "save");

    return (Long) getHibernateTemplate().save(entity);
}

public void saveOrUpdate(E entity) {
    LOG.entering(CLASS_NAME, "saveOrUpdate", entity);

    getHibernateTemplate().saveOrUpdate(entity);

    LOG.exiting(CLASS_NAME, "saveOrUpdate");
}

public void update(E entity) {
    LOG.entering(CLASS_NAME, "update", entity);

    getHibernateTemplate().update(entity);

    LOG.exiting(CLASS_NAME, "update");
}

/**
 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
 */
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    setStaticApplicationContext(applicationContext);
}

private static void setStaticApplicationContext(ApplicationContext applicationContext) {
    _applicationContext = applicationContext;
}

/**
 * @param queryName
 * @return count
 */
protected int getCounter(String queryName) {
    @SuppressWarnings("unchecked")
    List<Number> counterList = getHibernateTemplate().findByNamedQuery(queryName);

    return counterList == null || counterList.isEmpty() ? 0 : counterList.iterator().next().intValue();
}

/**
 * @param queryName
 * @param paramName
 * @param value
 * @return count
 */
protected int getCounter(String queryName, String paramName, Object value) {
    @SuppressWarnings("unchecked")
    List<Number> counterList = getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramName, value);

    return counterList == null || counterList.isEmpty() ? 0 : counterList.iterator().next().intValue();
}

/**
 * @param queryName
 * @param paramNames
 * @param values
 * @return count
 */
protected int getCounter(String queryName, String[] paramNames, Object[] values) {
    @SuppressWarnings("unchecked")
    List<Number> counterList = getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramNames, values);

    return counterList == null || counterList.isEmpty() ? 0 : counterList.iterator().next().intValue();
}

public List<E> narrowSearch(String keyword) {
    List<E> result = Collections.EMPTY_LIST;
    if (StringUtils.isBlank(keyword)) {
        return result;
    }
    try {
        Method method = _entityClass.getMethod("SEARCHABLE_FIELDS");
        if (method == null) {
            throw new RuntimeException(_entityClass + " should respond to static method call - SEARCHABLE_FIELDS");
        }
        result = narrowSearch(keyword, (String[]) method.invoke(null, null), _entityClass);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}

public List<E> search(String keyword) {
    List<E> result = Collections.EMPTY_LIST;
    if (StringUtils.isBlank(keyword)) {
        return result;
    }
    try {
        Method method = _entityClass.getMethod("SEARCHABLE_FIELDS");
        if (method == null) {
            throw new RuntimeException(_entityClass + " should respond to static method call - SEARCHABLE_FIELDS");
        }
        result = search(keyword, (String[]) method.invoke(null, null), _entityClass);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}

private List<E> search(String keyword, String[] fields, Class clazz) {
    Map<String, Integer> paginationOptions = Application.getPaginationOptions();
    Integer pageNumber = Integer.valueOf(1);
    Integer perPage = Integer.valueOf(5);

    if (paginationOptions.containsKey("perPage")) {
        pageNumber = paginationOptions.get("pageNumber");
        perPage = paginationOptions.get("perPage");
    }

    FullTextSession fullTextSession = Search.getFullTextSession(getHibernateTemplate().getSessionFactory().getCurrentSession());

    // create native Lucene query
    MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
    org.apache.lucene.search.Query query;
    try {
        query = parser.parse(keyword);
        // wrap Lucene query in a org.hibernate.Query
        org.hibernate.search.FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, clazz);
        hibQuery.setFirstResult((pageNumber - 1) * perPage);
        hibQuery.setMaxResults(perPage);
        Application.setResultSize(hibQuery.getResultSize());
        // execute search
        return hibQuery.list();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

private List<E> narrowSearch(String keyword, String[] fields, Class clazz) {
    /**
     * Need to identify better way of doing this for performance reasons.
     */
    List<E> results = new ArrayList<E>();
    for (String word : keyword.split(" ")) {
        if (results.isEmpty()) {
            results.addAll(search(word, fields, clazz));
        } else {
            results.retainAll(search(word, fields, clazz));
        }
    }
    return results;
}

protected static <T extends AbstractDAO<?>> T getDAO(String beanName, Class<T> clazz) {
    return clazz.cast(_applicationContext.getBean(beanName, clazz));
}


protected int getCount(List<Number> counterList) {
    return counterList == null || counterList.isEmpty() ? 0 : counterList.iterator().next().intValue();
}

@SuppressWarnings("unchecked")
protected List paginateByNamedQueryAndNamedParam(String hqlName, Map<String, Object> params) {
    Query namedQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().getNamedQuery(hqlName);
    configurePagination(namedQuery);

    for (Entry<String, Object> parameter : params.entrySet()) {
        if (parameter.getValue() instanceof Collection) {
            namedQuery.setParameterList(parameter.getKey(), (Collection) parameter.getValue());
        } else if (parameter.getValue() instanceof Object[]) {
            namedQuery.setParameterList(parameter.getKey(), (Object[]) parameter.getValue());
        } else {
            namedQuery.setParameter(parameter.getKey(), parameter.getValue());
        }

    }
    return namedQuery.list();
}

@SuppressWarnings("unchecked")
protected List paginateByNamedQuery(String hqlName) {
    Query namedQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().getNamedQuery(hqlName);
    configurePagination(namedQuery);

    List result = namedQuery.list();

    resetPagination(namedQuery);

    return result;
}

private void resetPagination(Query namedQuery) {
    getHibernateTemplate().setMaxResults(0);
}

private void configurePagination(Query namedQuery) {
    Integer pageNumber = Application.getPaginationOptions().get("pageNumber");
    Integer perPage = Application.getPaginationOptions().get("perPage");

    if (pageNumber != null && pageNumber > 1) {
        namedQuery.setFirstResult((pageNumber - 1) * perPage);
    }
    namedQuery.setMaxResults(perPage == null ? 5 : perPage);
}



}

Use this one. It has pagination as well as lucene integration. I created this one for one of my projects. Make sure every DAO extends from it. Take out what you do not need.

package com.isavera.hibernate;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.criterion.Criterion;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.isavera.actions.utils.Application;

@SuppressWarnings("serial")
@Transactional
public abstract class AbstractDAOImpl<E> extends HibernateDaoSupport implements AbstractDAO<E>, ApplicationContextAware {


private static final String CLASS_NAME = AbstractDAOImpl.class.getName();

private static final Logger LOG = Logger.getLogger(CLASS_NAME);

private static ApplicationContext _applicationContext;

private final Class<? extends E> _entityClass;

/**
 * @param entityClass
 */
public AbstractDAOImpl(Class<? extends E> entityClass) {
    super();

    _entityClass = entityClass;
}


public void delete(E entity) {
    LOG.entering(CLASS_NAME, "delete", entity);

    getHibernateTemplate().delete(entity);

    LOG.exiting(CLASS_NAME, "delete");
}

public void evict(E entity) {
    LOG.entering(CLASS_NAME, "evict", entity);

    getHibernateTemplate().evict(entity);

    LOG.exiting(CLASS_NAME, "evict");
}

public void deleteAll(Collection<E> entities) {
    getHibernateTemplate().deleteAll(entities);
}

@SuppressWarnings("unchecked")
public List<E> findByNamedQuery(String queryName) {
    return getHibernateTemplate().findByNamedQuery(queryName);
}

@SuppressWarnings("unchecked")
public List<E> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value) {
    return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramName, value);
}

@SuppressWarnings("unchecked")
public List<E> findByNamedQueryAndNamedParam(String queryName, String[] paramNames, Object[] values) {
    return getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramNames, values);
}

public E get(Serializable id) {
    LOG.entering(CLASS_NAME, "get", id);

    @SuppressWarnings("unchecked")
    E entity = (E) getHibernateTemplate().get(_entityClass, id);

    LOG.exiting(CLASS_NAME, "get", entity);

    return entity;
}

public List<E> get(Criterion... criterion) {
    LOG.entering(CLASS_NAME, "get", criterion);

    Criteria criteria = getSession().createCriteria(_entityClass);

    for (Criterion c : criterion) {
        criteria.add(c);
    }

    @SuppressWarnings("unchecked")
    List<E> list = new ArrayList<E>(criteria.list());

    LOG.exiting(CLASS_NAME, "get", list);

    return list;
}

public boolean isEntityAttached(E entity) {
    return getHibernateTemplate().getSessionFactory().getCurrentSession().contains(entity);
}

public E load(Serializable id) {
    LOG.entering(CLASS_NAME, "load", id);

    @SuppressWarnings("unchecked")
    E entity = (E) getHibernateTemplate().load(_entityClass, id);

    LOG.exiting(CLASS_NAME, "load", entity);

    return entity;
}

public E load(Serializable id, LockMode lockMode) {
    LOG.entering(CLASS_NAME, "load", new Object[] { id, lockMode });

    @SuppressWarnings("unchecked")
    E entity = (E) getHibernateTemplate().load(_entityClass, id, lockMode);

    LOG.exiting(CLASS_NAME, "load", entity);

    return entity;
}

public void load(E entity, Serializable id) {
    LOG.entering(CLASS_NAME, "load", new Object[] { entity, id });

    getHibernateTemplate().load(entity, id);

    LOG.exiting(CLASS_NAME, "load");
}

public void lock(E entity, LockMode lockMode) {
    LOG.entering(CLASS_NAME, "lock", new Object[] { entity, lockMode });

    getHibernateTemplate().lock(entity, lockMode);

    LOG.exiting(CLASS_NAME, "lock");
}



public void saveOrUpdateAll(Collection<E> entities) {
    getHibernateTemplate().saveOrUpdateAll(entities);
}

@SuppressWarnings("unchecked")
public E merge(E entity) {
    LOG.entering(CLASS_NAME, "merge", entity);

    E persistentEntity = (E) getHibernateTemplate().merge(entity);

    LOG.exiting(CLASS_NAME, "merge", persistentEntity);

    return persistentEntity;
}

public void refresh(E entity) {
    LOG.entering(CLASS_NAME, "refresh", entity);

    getHibernateTemplate().refresh(entity);

    LOG.exiting(CLASS_NAME, "refresh");
}

public Long save(E entity) {
    LOG.entering(CLASS_NAME, "save", entity);

    LOG.exiting(CLASS_NAME, "save");

    return (Long) getHibernateTemplate().save(entity);
}

public void saveOrUpdate(E entity) {
    LOG.entering(CLASS_NAME, "saveOrUpdate", entity);

    getHibernateTemplate().saveOrUpdate(entity);

    LOG.exiting(CLASS_NAME, "saveOrUpdate");
}

public void update(E entity) {
    LOG.entering(CLASS_NAME, "update", entity);

    getHibernateTemplate().update(entity);

    LOG.exiting(CLASS_NAME, "update");
}

/**
 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
 */
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    setStaticApplicationContext(applicationContext);
}

private static void setStaticApplicationContext(ApplicationContext applicationContext) {
    _applicationContext = applicationContext;
}

/**
 * @param queryName
 * @return count
 */
protected int getCounter(String queryName) {
    @SuppressWarnings("unchecked")
    List<Number> counterList = getHibernateTemplate().findByNamedQuery(queryName);

    return counterList == null || counterList.isEmpty() ? 0 : counterList.iterator().next().intValue();
}

/**
 * @param queryName
 * @param paramName
 * @param value
 * @return count
 */
protected int getCounter(String queryName, String paramName, Object value) {
    @SuppressWarnings("unchecked")
    List<Number> counterList = getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramName, value);

    return counterList == null || counterList.isEmpty() ? 0 : counterList.iterator().next().intValue();
}

/**
 * @param queryName
 * @param paramNames
 * @param values
 * @return count
 */
protected int getCounter(String queryName, String[] paramNames, Object[] values) {
    @SuppressWarnings("unchecked")
    List<Number> counterList = getHibernateTemplate().findByNamedQueryAndNamedParam(queryName, paramNames, values);

    return counterList == null || counterList.isEmpty() ? 0 : counterList.iterator().next().intValue();
}

public List<E> narrowSearch(String keyword) {
    List<E> result = Collections.EMPTY_LIST;
    if (StringUtils.isBlank(keyword)) {
        return result;
    }
    try {
        Method method = _entityClass.getMethod("SEARCHABLE_FIELDS");
        if (method == null) {
            throw new RuntimeException(_entityClass + " should respond to static method call - SEARCHABLE_FIELDS");
        }
        result = narrowSearch(keyword, (String[]) method.invoke(null, null), _entityClass);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}

public List<E> search(String keyword) {
    List<E> result = Collections.EMPTY_LIST;
    if (StringUtils.isBlank(keyword)) {
        return result;
    }
    try {
        Method method = _entityClass.getMethod("SEARCHABLE_FIELDS");
        if (method == null) {
            throw new RuntimeException(_entityClass + " should respond to static method call - SEARCHABLE_FIELDS");
        }
        result = search(keyword, (String[]) method.invoke(null, null), _entityClass);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return result;
}

private List<E> search(String keyword, String[] fields, Class clazz) {
    Map<String, Integer> paginationOptions = Application.getPaginationOptions();
    Integer pageNumber = Integer.valueOf(1);
    Integer perPage = Integer.valueOf(5);

    if (paginationOptions.containsKey("perPage")) {
        pageNumber = paginationOptions.get("pageNumber");
        perPage = paginationOptions.get("perPage");
    }

    FullTextSession fullTextSession = Search.getFullTextSession(getHibernateTemplate().getSessionFactory().getCurrentSession());

    // create native Lucene query
    MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
    org.apache.lucene.search.Query query;
    try {
        query = parser.parse(keyword);
        // wrap Lucene query in a org.hibernate.Query
        org.hibernate.search.FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, clazz);
        hibQuery.setFirstResult((pageNumber - 1) * perPage);
        hibQuery.setMaxResults(perPage);
        Application.setResultSize(hibQuery.getResultSize());
        // execute search
        return hibQuery.list();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

private List<E> narrowSearch(String keyword, String[] fields, Class clazz) {
    /**
     * Need to identify better way of doing this for performance reasons.
     */
    List<E> results = new ArrayList<E>();
    for (String word : keyword.split(" ")) {
        if (results.isEmpty()) {
            results.addAll(search(word, fields, clazz));
        } else {
            results.retainAll(search(word, fields, clazz));
        }
    }
    return results;
}

protected static <T extends AbstractDAO<?>> T getDAO(String beanName, Class<T> clazz) {
    return clazz.cast(_applicationContext.getBean(beanName, clazz));
}


protected int getCount(List<Number> counterList) {
    return counterList == null || counterList.isEmpty() ? 0 : counterList.iterator().next().intValue();
}

@SuppressWarnings("unchecked")
protected List paginateByNamedQueryAndNamedParam(String hqlName, Map<String, Object> params) {
    Query namedQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().getNamedQuery(hqlName);
    configurePagination(namedQuery);

    for (Entry<String, Object> parameter : params.entrySet()) {
        if (parameter.getValue() instanceof Collection) {
            namedQuery.setParameterList(parameter.getKey(), (Collection) parameter.getValue());
        } else if (parameter.getValue() instanceof Object[]) {
            namedQuery.setParameterList(parameter.getKey(), (Object[]) parameter.getValue());
        } else {
            namedQuery.setParameter(parameter.getKey(), parameter.getValue());
        }

    }
    return namedQuery.list();
}

@SuppressWarnings("unchecked")
protected List paginateByNamedQuery(String hqlName) {
    Query namedQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().getNamedQuery(hqlName);
    configurePagination(namedQuery);

    List result = namedQuery.list();

    resetPagination(namedQuery);

    return result;
}

private void resetPagination(Query namedQuery) {
    getHibernateTemplate().setMaxResults(0);
}

private void configurePagination(Query namedQuery) {
    Integer pageNumber = Application.getPaginationOptions().get("pageNumber");
    Integer perPage = Application.getPaginationOptions().get("perPage");

    if (pageNumber != null && pageNumber > 1) {
        namedQuery.setFirstResult((pageNumber - 1) * perPage);
    }
    namedQuery.setMaxResults(perPage == null ? 5 : perPage);
}



}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文