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

net.sourceforge.kivu4j.utils.hibernate.HibernateGenericRepository 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.io.Serializable;
import java.util.Collection;
import java.util.Date;

import javax.annotation.Resource;

import net.sourceforge.kivu4j.utils.api.repository.ExampleParameter;
import net.sourceforge.kivu4j.utils.api.repository.FilterParameter;
import net.sourceforge.kivu4j.utils.api.repository.GenericRepository;
import net.sourceforge.kivu4j.utils.api.repository.QueryParameter;
import net.sourceforge.kivu4j.utils.lang.domain.Persistent;

import org.hibernate.criterion.DetachedCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * 泛型的hibernate数据访问接口
 *
 * @author Lucky Yang
 */
public abstract class HibernateGenericRepository

, PK extends Serializable> implements GenericRepository { protected Logger logger = LoggerFactory.getLogger(getClass()); @Resource protected HibernateTemplate hibernateTemplate; protected abstract Class

getEntityClass(); @Override @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) public P findById(final PK id) { Assert.notNull(id, "'id' must not null"); return (P) this.hibernateTemplate.get(this.getEntityClass(), id); } @Override @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) public P findReferenceById(final PK id) { Assert.notNull(id, "'id' must not null"); return (P) this.hibernateTemplate.load(this.getEntityClass(), id); } @Override @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) public boolean exist(PK id) { return (this.findById(id) != null); } @Override @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) public P findRequiredById(final PK id) { if (!this.exist(id)) throw new ObjectRetrievalFailureException(this.getEntityClass(), id); return this.findById(id); } @Override @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public P save(final P entity) { Assert.notNull(entity, "'entity' must not null"); if (entity.isPersist()) entity.setModifyOn(new Date()); else entity.setCreateOn(new Date()); this.hibernateTemplate.saveOrUpdate(entity); if (logger.isDebugEnabled()) logger.debug("entity saved: {}", entity); return entity; } @Override @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public void remove(final P entity) { Assert.notNull(entity, "'entity' must not null"); if (logger.isDebugEnabled()) logger.debug("deleting entity: {}", entity); this.hibernateTemplate.delete(entity); } @Override @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public void remove(final PK id) { Assert.notNull(id, "'id' must not null"); this.remove(this.findRequiredById(id)); } protected void initProxy(Object proxy) { Assert.notNull(proxy, "'proxy' must not null"); this.hibernateTemplate.initialize(proxy); } @Override @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public int bulk(QueryParameter parameter){ Assert.notNull(parameter, "'parameter' must not null"); Assert.isTrue(parameter.isValid(), "'parameter' is failure"); int result = this.hibernateTemplate.execute( new BulkExecutorCallback(parameter) ); if (this.logger.isDebugEnabled()) logger.debug("{} row(s) effect , sql:{}", result, parameter.toString()); return result; } @Override @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) public Collection find(QueryParameter parameter, Class clazz){ Assert.notNull(parameter, "'parameter' must not null"); Assert.notNull(clazz, "'clazz' must not null"); Assert.isTrue(parameter.isValid(), "'parameter' is failure"); parameter.setDistanct(false); Collection result = this.hibernateTemplate.execute( new QueryParameterExecutorCallback(parameter) ); if (this.logger.isDebugEnabled()) logger.debug("{} row(s) effect , sql:{}", result.size(), parameter.toString()); return result; } @Override @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) public Collection

find(QueryParameter parameter){ Assert.notNull(parameter, "'parameter' must not null"); Assert.isTrue(parameter.isValid(), "'parameter' is failure"); Collection

result = this.hibernateTemplate.execute( new QueryParameterExecutorCallback

(parameter) ); if (this.logger.isDebugEnabled()) logger.debug("{} row(s) effect , sql:{}", result.size(), parameter.toString()); return result; } @Override @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) public Collection

find(FilterParameter parameter) { Assert.notNull(parameter, "'parameter' must not null"); Assert.isTrue(parameter.isValid(), "'parameter' is failure"); Collection

result = this.hibernateTemplate.execute( new FilterParameterExecutorCallback

(this.getEntityClass(), parameter) ); if (this.logger.isDebugEnabled()) logger.debug("{} row(s) effect , sql:{}", result.size(), parameter.toString()); return result; } @Override @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) public Collection

find(ExampleParameter parameter) { Assert.notNull(parameter, "'parameter' must not null"); Assert.isTrue(parameter.isValid(), "'parameter' is failure"); Collection

result = this.hibernateTemplate.execute( new ExampleParameterExecutorCallback

(this.getEntityClass(),parameter) ); if (this.logger.isDebugEnabled()) logger.debug("{} row(s) effect , sql:{}", result.size(), parameter.toString()); return result; } @Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED) protected Collection

find(DetachedCriteria detachedCriteria) { Assert.notNull(detachedCriteria, "'detachedCriteria' must not null"); Collection

result = this.hibernateTemplate.execute( new CriteriaExecutorCallback

(detachedCriteria) ); if (this.logger.isDebugEnabled()) logger.debug("{} row(s) effect, sql:{}", result.size(), detachedCriteria.toString()); return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy