cn.newgxu.lab.core.repository.impl.AbstractCommonDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for cn.newgxu.lab.core.repository.impl.AbstractCommonDaoImpl.java

Source

/*
 * Copyright (c) 2001-2013 newgxu.cn <the original author or authors>.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package cn.newgxu.lab.core.repository.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import cn.newgxu.lab.core.repository.CommonDao;

/**
 * ?????dao
 * 
 * @author longkai
 * @email im.longkai@gmail.com
 * @since 2013-3-28
 * @version 0.1
 */
public abstract class AbstractCommonDaoImpl<T> implements CommonDao<T> {

    /**  */
    protected static final Logger L = LoggerFactory.getLogger(AbstractCommonDaoImpl.class);

    @PersistenceContext
    protected EntityManager em;

    @Override
    public void persist(T entity) {
        Assert.notNull(entity, "??");
        em.persist(entity);
    }

    @Override
    public void remove(T entity) {
        Assert.notNull(entity, "??");
        em.remove(entity);
    }

    @Override
    public void merge(T entity) {
        Assert.notNull(entity, "??");
        em.merge(entity);
    }

    /**
     * 
     * @param clazz 
     * @return 
     */
    protected long size(Class<?> clazz) {
        return em.createQuery("SELECT count(*) FROM " + clazz.getSimpleName(), Long.class).getSingleResult();
    }

    /**
     * ??<b style="color: red;"> ? </b>????
     * @param hql hql
     * @param type 
     * @param objects ?, null?
     * @return ?
     */
    protected T executeQuery(String hql, Class<T> clazz, Object... objects) {
        TypedQuery<T> query = em.createQuery(hql, clazz);
        if (objects != null) {
            for (int i = 0; i < objects.length; i++) {
                query.setParameter(i + 1, objects[i]);
            }
        }
        return query.getSingleResult();
    }

    /**
     * ??<b style="color: red;"> :xxx </b>?????
     * @param hql ?hql
     * @param type 
     * @param objects ?map, ?
     * @return ?
     */
    protected T executeQuery(String hql, Class<T> clazz, Map<String, Object> params) {
        TypedQuery<T> query = em.createQuery(hql, clazz);
        if (params != null) {
            for (String name : params.keySet()) {
                query.setParameter(name, params.get(name));
            }
        }
        return query.getSingleResult();
    }

    /**
     * ?<b style="color: red;"> ? </b>?????
     * @param hql hql
     * @param clazz 
     * @param offset ??????
     * @param number ????
     * @param objects ?null?
     * @return 
     */
    protected List<T> executeQuery(String hql, Class<T> clazz, int offset, int number, Object... objects) {
        offset = rangeCheck(offset, clazz);
        TypedQuery<T> query = em.createNamedQuery(hql, clazz);
        if (objects != null) {
            for (int i = 0; i < objects.length; i++) {
                query.setParameter(i + 1, objects[i]);
            }
        }
        return query.setFirstResult(offset).setMaxResults(number).getResultList();
    }

    /**
     * ?<b style="color: red;"> :xxx </b>?????
     * @param hql hql
     * @param clazz 
     * @param offset ??????
     * @param number ????
     * @param params ?map?
     * @return 
     */
    protected List<T> executeQuery(String hql, Class<T> clazz, int offset, int number, Map<String, Object> params) {
        offset = rangeCheck(offset, clazz);
        TypedQuery<T> query = em.createNamedQuery(hql, clazz);
        if (params != null) {
            for (String name : params.keySet()) {
                query.setParameter(name, params.get(name));
            }
        }
        return query.setFirstResult(offset).setMaxResults(number).getResultList();
    }

    /**
     * ????
     * 
     * @param offset ???
     * @param clazz ?
     * @return ????
     */
    protected int rangeCheck(int offset, Class<?> clazz) {
        if (offset < 0) {
            offset = 0;
            return offset;
        }
        long size = size(clazz);
        if (offset > size) {
            offset = Long.valueOf(size).intValue();
        }
        return offset;
    }

}