package com.iaspec.webgis.dao.impl;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.iaspec.webgis.dao.GenericDao;
import com.iaspec.webgis.util.GenericsUtils;
public class GenericDaoImpl<T, PK extends Serializable> implements
GenericDao<T, PK> {
protected final Log log = LogFactory.getLog(getClass());
private Class<T> persistentClass;
public Class<T> getPersistentClass(){
return persistentClass;
}
@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactoryTemp;
@Autowired
@Qualifier("hibernateTemplate")
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate(){
if(hibernateTemplate==null){
hibernateTemplate=new HibernateTemplate();
hibernateTemplate.setSessionFactory(sessionFactoryTemp);
}
return hibernateTemplate;
}
@SuppressWarnings("unchecked")
public GenericDaoImpl() {
this.persistentClass = GenericsUtils.getSuperClassGenricType(getClass());
}
@SuppressWarnings("unchecked")
public List<T> getAll() {
return getHibernateTemplate().loadAll(this.persistentClass);
}
@SuppressWarnings("unchecked")
public T get(PK id) {
//get(String id)
T entity = (T) 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;
}
@SuppressWarnings("unchecked")
public boolean exists(PK id) {
T entity = (T) getHibernateTemplate().get(this.persistentClass, id);
return entity != null;
}
@SuppressWarnings("unchecked")
public T save(T object) {
return (T)getHibernateTemplate().merge(object);
}
public void remove(PK id) {
getHibernateTemplate().delete(this.get(id));
}
public List findByhql(String hql) {
List tempList=null;
try{
tempList=this.getHibernateTemplate().find(hql);
}catch(Exception e){
System.out.println("执行Hql出错:"+e);
}
return tempList;
}
public List<T> getListByHql(String hql) {
return findByhql(hql);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
junit+hibernate3.2+spring2.5工程
共40个文件
java:15个
class:15个
xml:5个
需积分: 10 19 下载量 71 浏览量
2008-09-04
16:35:39
上传
评论
收藏 26KB RAR 举报
温馨提示
直接拷贝到eclipse的空间目录下,加入常见的jar包,和hibernate,junit,spring的包,就可以对其spring ,hibernate进行测试。
资源推荐
资源详情
资源评论
收起资源包目录
testUniversal.rar (40个子文件)
testUniversal
test
service
BaseTest.java 389B
TestChild.java 532B
src
com
iaspec
webgis
dao
ChildDao.java 197B
UniversalDao.java 270B
GenericDao.java 342B
impl
ChildDaoImpl.java 522B
UniversalDaoImpl.java 2KB
GenericDaoImpl.java 3KB
bean
Parent.java 682B
Child.java 665B
service
ChildManager.java 167B
impl
ChildManagerImpl.java 894B
BaseManagerImpl.java 986B
BaseManager.java 236B
util
GenericsUtils.java 2KB
hibernate.cfg.xml 382B
applicationContext.xml 5KB
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 1KB
classes
com
iaspec
webgis
dao
GenericDao.class 698B
ChildDao.class 344B
UniversalDao.class 376B
impl
UniversalDaoImpl.class 2KB
ChildDaoImpl.class 1KB
GenericDaoImpl.class 4KB
bean
Child.class 1KB
Parent.class 1KB
service
BaseManager.class 327B
ChildManager.class 253B
impl
ChildManagerImpl.class 2KB
BaseManagerImpl.class 1KB
util
GenericsUtils.class 1KB
service
TestChild.class 1KB
BaseTest.class 641B
hibernate.cfg.xml 382B
applicationContext.xml 5KB
lib
index.jsp 834B
.mymetadata 312B
.classpath 5KB
.myeclipse
共 40 条
- 1
资源评论
starting0
- 粉丝: 14
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功