惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
I
InfoQ
博客园_首页
G
Google Developers Blog
爱范儿
爱范儿
Last Week in AI
Last Week in AI
量子位
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
月光博客
月光博客
The GitHub Blog
The GitHub Blog
V
Visual Studio Blog
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - sunliho

Java ClassLoader and Context ClassLoader find flow scope varibles in spring web flow Building Liferay on Tomcat Debugging Liferay in Eclipse ssl 服务器证书,客户端信任证书 相对路径与绝对路径 - sunliho - 博客园 db2 command 标准sql Linux系统下架设APACHE SVN服务器全过程 修改ip地址 mysql in linux network configuration in linux Linux系统信息查看命令大全 linux常用命令 Permission denied in linux install jdk in linux hibernate.cfg.xml 配置(摘录) - sunliho - 博客园 hibernate 配置文件(摘录) - sunliho - 博客园 web.xml配置详解(摘录)
generic dao
sunliho · 2010-08-10 · via 博客园 - sunliho

package flight.dao;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.hibernate.Query;
import org.hibernate.SessionFactory;


public class GenericDao<T> {
 private SessionFactory sessionFactory ;

 public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }
 
 @SuppressWarnings("unchecked")
 public  T get(Serializable id,Class<T> type){
  return (T) this.sessionFactory.getCurrentSession().get(type.getClass(), id);
 }
 
 public Serializable save(T t){
  return this.sessionFactory.getCurrentSession().save(t) ;
 }
 
 public void persist(T t){
  this.sessionFactory.getCurrentSession().persist(t) ;
 }
 
 public void delete(T t){
  this.sessionFactory.getCurrentSession().delete(t);
 }
 
 public void update(T t){
  this.sessionFactory.getCurrentSession().update(t);
 }
 
 @SuppressWarnings("unchecked")
 public List<Object> findByNamedQuery(String namedQueryName){
  return this.sessionFactory.getCurrentSession().getNamedQuery(namedQueryName).list();
 }
 
 @SuppressWarnings("unchecked")
 public List<Object> findByNamedQuery(String namedQueryName,int resultLimit){
  return this.sessionFactory.getCurrentSession()
  .getNamedQuery(namedQueryName).setMaxResults(resultLimit).list();
 }
 
 public List<Object> findByNamedQuery(String namedQueryName,
   Map<String, Object> parameters) {
  return this.findByNamedQuery(namedQueryName, parameters,0, 0);
 }
 
 @SuppressWarnings("unchecked")
 public List<Object> findByNamedQuery(String namedQueryName,
   Map<String, Object> parameters,int startIndex,int resultLimit) {
  Query query = this.sessionFactory.getCurrentSession().getNamedQuery(
    namedQueryName);
  Set<Entry<String, Object>> rawParameters = parameters.entrySet();

  for (Entry<String, Object> entry : rawParameters) {
   query.setParameter(entry.getKey(), entry.getValue());
  }
  
  if (startIndex != 0)
   query.setFirstResult(startIndex);
  
  if(resultLimit > 0)
   query.setMaxResults(resultLimit);
  
  return query.list();
 }
 
 
 
 
 

}