⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericdaohibernate.java

📁 基于Maven的质量保证自动化环境配置和演示程序
💻 JAVA
字号:
package com.cib.dao.hibernate;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import com.cib.dao.GenericDao;import org.springframework.orm.ObjectRetrievalFailureException;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import java.io.Serializable;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.LinkedHashSet;import java.util.List;import java.util.Map;/** * This class serves as the Base class for all other DAOs - namely to hold * common CRUD methods that they might all use. You should only need to extend * this class when your require custom CRUD logic. * * <p>To register this class in your Spring context file, use the following XML. * <pre> *      &lt;bean id="fooDao" class="com.cib.dao.hibernate.GenericDaoHibernate"&gt; *          &lt;constructor-arg value="com.cib.model.Foo"/&gt; *          &lt;property name="sessionFactory" ref="sessionFactory"/&gt; *      &lt;/bean&gt; * </pre> * * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> * @param <T> a type variable * @param <PK> the primary key for that type */public class GenericDaoHibernate<T, PK extends Serializable> extends HibernateDaoSupport implements GenericDao<T, PK> {    /**     * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging     */    protected final Log log = LogFactory.getLog(getClass());    private Class<T> persistentClass;    /**     * @param persistentClass 要被保存到数据库的类     */    public GenericDaoHibernate(final Class<T> persistentClass) {        this.persistentClass = persistentClass;    }    /**     * {@inheritDoc}     */    @SuppressWarnings("unchecked")    public List<T> getAll() {        return super.getHibernateTemplate().loadAll(this.persistentClass);    }        /**     * {@inheritDoc}     */    @SuppressWarnings("unchecked")    public List<T> getAllDistinct() {        Collection result = new LinkedHashSet(getAll());        return new ArrayList(result);    }        /**     * {@inheritDoc}     */    @SuppressWarnings("unchecked")    public T get(PK id) {        T entity = (T) super.getHibernateTemplate().get(this.persistentClass, id);        if (entity == null) {            log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");            throw new ObjectRetrievalFailureException(this.persistentClass, id);        }        return entity;    }    /**     * {@inheritDoc}     */    @SuppressWarnings("unchecked")    public boolean exists(PK id) {        T entity = (T) super.getHibernateTemplate().get(this.persistentClass, id);        return entity != null;    }    /**     * {@inheritDoc}     */    @SuppressWarnings("unchecked")    public T save(T object) {        return (T) super.getHibernateTemplate().merge(object);    }    /**     * {@inheritDoc}     */    public void remove(PK id) {        super.getHibernateTemplate().delete(this.get(id));    }       /**     * {@inheritDoc}    */   @SuppressWarnings("unchecked")   public List<T> findByNamedQuery(       String queryName,        Map<String, Object> queryParams) {       String []params = new String[queryParams.size()];       Object []values = new Object[queryParams.size()];       int index = 0;       Iterator<String> i = queryParams.keySet().iterator();       while (i.hasNext()) {           String key = i.next();           params[index] = key;           values[index++] = queryParams.get(key);       }       return getHibernateTemplate().findByNamedQueryAndNamedParam(           queryName,            params,            values);   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -