`
elicer
  • 浏览: 131554 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

基于 Open Jpa 的 GenericDao 的实现

    博客分类:
  • JPA
阅读更多
IGenericDao  Class
public interface IGenericDao<T, ID extends Object> {   
  
    public T load(ID id) throws DataAccessException;   
  
    public T get(ID id) throws DataAccessException;   
  
    public boolean contains(T t) throws DataAccessException;   
  
    public void refresh(T t, LockMode lockMode) throws DataAccessException;   
  
    public void refresh(T t) throws DataAccessException;   
  
    public void save(T t) throws DataAccessException;   
  
    public void saveOrUpdate(T t) throws DataAccessException;   
  
    public void saveOrUpdateAll(Collection<T> entities) throws DataAccessException;   
  
    public void update(T t, LockMode lockMode) throws DataAccessException;   
  
    public void update(T t) throws DataAccessException;   
  
//    public void delete(T t, LockMode lockMode) throws DataAccessException;   
  
    public void delete(ID t) throws DataAccessException;   
  
    public void deleteAll(Collection<T> entities) throws DataAccessException;   
  
    public List<T> find(String queryString, Object value) throws DataAccessException;   
  
    public List<T> find(String queryString, Object[] values) throws DataAccessException;   
  
    public List<T> find(String queryString) throws DataAccessException;   
  
    public List<T> list() throws DataAccessException;   
  
//    public List<T> findByNamedQuery(String queryName) throws DataAccessException;   
//  
//    public List<T> findByNamedQuery(String queryName, Object value) throws DataAccessException;   
  
    public List<T> findByNamedQuery(String queryName, Object... values) throws DataAccessException;   
  
//    public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,   
//            final int startIndex);   
//  
//    public PaginationSupport findPageByQuery(final String hql, final String countHql, final int pageSize,   
//            final int startIndex);   
  
}  





JpaGenericDao  Class

import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.List;

import org.hibernate.LockMode;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.jpa.support.JpaDaoSupport;
@SuppressWarnings("unchecked")
public  class JPAGenericDao<T, ID extends Object> extends JpaDaoSupport implements IGenericDao<T, ID> {

	
    protected Class<T> entityClass;   
    
    public JPAGenericDao() {   
  
    }   
  
    protected Class getEntityClass() {   
        if (entityClass == null) {   
            entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];   
            logger.debug("T class = " + entityClass.getName());   
        }   
        return entityClass;   
    } 
	public boolean contains(T t) throws DataAccessException {
		// TODO Auto-generated method stub
		return this.getJpaTemplate().contains(t);
	}

	public void delete(ID id) throws DataAccessException {
		// TODO Auto-generated method stub
        // 使用Query删除对象   
		this.getJpaTemplate().remove(this.getJpaTemplate().find(getEntityClass(), id));
 
	}


	public void deleteAll(Collection<T> entities) throws DataAccessException {
		// TODO Auto-generated method stub
       
	}

	public List<T> find(String queryString, Object value)
			throws DataAccessException {
		// TODO Auto-generated method stub
		return this.getJpaTemplate().find(queryString, value);
	}

	public List<T> find(String queryString, Object[] values)
			throws DataAccessException {
		// TODO Auto-generated method stub
		List<T> find = (List<T>)this.getJpaTemplate().find(queryString, values);
		return find;
	}

	public List<T> find(String queryString) throws DataAccessException {
		// TODO Auto-generated method stub
		List<T> find = (List<T>)this.getJpaTemplate().find(queryString);
		return find;
	}





	public T get(Object id) throws DataAccessException {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> list() throws DataAccessException {
		// TODO Auto-generated method stub
		
		return this.getJpaTemplate().getEntityManager().createQuery("select all from " + this.getEntityClass().getSimpleName() + " all ").getResultList();
 

		
	 }

	public T load(Object id) throws DataAccessException {
		// TODO Auto-generated method stub
		return (T)this.getJpaTemplate().find(this.getEntityClass(), id);
	}

	public void refresh(T t, LockMode lockMode) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	public void refresh(T t) throws DataAccessException {
		// TODO Auto-generated method stub
        this.getJpaTemplate().refresh(t);
	}

	public void save(T t) throws DataAccessException {
		// TODO Auto-generated method stub
		this.getJpaTemplate().persist(t);
	}

	public void saveOrUpdate(T t) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	public void saveOrUpdateAll(Collection entities) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	public void update(T t, LockMode lockMode) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	public void update(T t) throws DataAccessException {
		// TODO Auto-generated method stub
        this.getJpaTemplate().merge(t);
	}


	public List<T> findByNamedQuery(String queryName, Object... values)
			throws DataAccessException {
		// TODO Auto-generated method stub

		return (List<T>)this.getJpaTemplate().findByNamedQuery(queryName, values);

	}
	



}





具体实现类  MagazineDaoImpl 实现起来非常简单

public class MagazineDaoImpl extends JPAGenericDao<Magazine,Magazine.MagazineId> implements MagazineDao


分享到:
评论

相关推荐

    Apache OpenJPA 2.1 User's Guide

    OpenJPA is Apache's implementation of Sun's Java Persistence 2.0 API (JSR-317 JPA 2.0) specification for the transparent persistence of Java objects. This document provides an overview of the JPA ...

    OpenJPA 2.2.1 API (CHM格式)

     OpenJPA 是 Apache 组织提供的开源项目,它实现了 EJB 3.0 中的 JPA 标准,为开发者提供功能强大、使用简单的持久化数据管理框架。OpenJPA 封装了和关系型数据库交互的操作,让开发者把注意力集中在编写业务逻辑上...

    openjpa范例及实例工程

    默认情况下,当应用程序第一次获取实体标识时,OpenJPA 框架从数据库中一次性获取 50 个连续的实体标识缓存起来,当下一次应用程序需要获取实体标识时,OpenJPA 将首先检测缓存中是否存在实体标识,如果存在,Open...

    基于springboot+jpa实现java后台api接口,点餐系统.zip

    基于springboot+jpa实现java后台api接口,点餐系统 基于springboot+jpa实现java后台api接口,点餐系统 基于springboot+jpa实现java后台api接口,点餐系统 基于springboot+jpa实现java后台api接口,点餐系统 基于...

    openJPA官方手册

    openJPA官方手册,openJPA官方手册openJPA官方手册

    Open JPA2 employee 简单例子

    Open JPA2 employee 简单例子

    OpenJPA API 文档 chm格式

    OpenJPA API 文档 chm格式

    openjpa 写的一个例子

    自己用openjpa些的一个基于web 的例子

    openjpa-manual

    JPA Overview and OpenJPA Reference Guide

    apache-openjpa-2.2.1-source

    OpenJPA 是 Apache 组织提供的开源项目,它实现了 EJB 3.0 中的 JPA 标准,为开发者提供功能强大、使用简单的持久化数据管理框架。OpenJPA 封装了和关系型数据库交互的操作,让开发者把注意力集中在编写业务逻辑上。...

    Spring中使用OpenJPA

    Spring中使用OpenJPA源代码 博文链接:https://lxy19791111.iteye.com/blog/184997

    jsf、openJpa学习

    jsf 入门知识 ,jsf标签,jsf与openJpa整合,jsf与Spring整合

    openjpa实体标识的生成策略

    OpenJPA 是 Apache 组织提供的 JPA 标准实现。本文是 使用 Apache OpenJPA 开发 EJB 3.0 应用系列 的第五部分,介绍在 OpenJPA 中如何自动生成实体的唯一标识,包括使用容器自动生成实体标识,借助于数据库的自动...

    Openjpa手册

    OpenJPA OpenJPA 是 Apache 组织提供的开源项目,它实现了 EJB 3.0 中的 JPA 标准,为开发者提供功能强大、使用简单的持久化数据管理框架。

    openjpa:Apache OpenJPA

    Apache OpenJPA是Java Persistence API规范的实现。 执照 此存储库的内容已根据Apache License 2.0 许可 更多信息 可以在openjpa-project子目录中找到以下文件: openjpa-project / BUILDING.txt openjpa-project...

    apache-openjpa-3.1.0-binary.zip

    JPA规范要求对实体对象进行某种类型的监视,但是该规范并未定义如何实现此监视。一些JPA提供程序动态生成在用户的Entity对象前面的新子类或代理对象,而其他JPA提供程序使用字节码编织技术来增强实际的Entity类对象...

    Spring MVC+OpenJPA框架

    Spring MVC+OpenJPA框架,只是个框架哦

    Openjpa2.2+Mysql+Maven+Servlet+JSP source code

    Openjpa2.2+Mysql+Maven+Servlet+JSP 博客源码: http://blog.csdn.net/shenhonglei1234/article/details/10394379

    JPA教程,包括TOPLink JPA,Hibernate JPA,Open Jpa,jpa批注

    包括TOPLink JPA,Hibernate JPA,Open Jpa,jpa批注

    毕设项目-基于springboot+jpa实现java后台api接口点餐系统源码.zip

    毕设项目-基于springboot+jpa实现java后台api接口点餐系统源码.zip毕设项目-基于springboot+jpa实现java后台api接口点餐系统源码.zip毕设项目-基于springboot+jpa实现java后台api接口点餐系统源码.zip毕设项目-基于...

Global site tag (gtag.js) - Google Analytics