`
xzknet
  • 浏览: 300248 次
  • 性别: Icon_minigender_1
  • 来自: 河南
文章分类
社区版块
存档分类
最新评论

解析eclipse下生成Hibernate DAO中的几个方法

阅读更多
 

      前几天用eclipse下生成的Hibernate DAO做了一个实现的增删查改的小例子,在这里解释下生成DAO中的几个方法到底是做什么用的.
这里我将以注释的形式在下面的这段java代码中解释.

package dao;
/** *//**
 * 很简单引入你要用的包
 
*/

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/** *//**
 * 类UsertableDAO继承了BaseHibernateDAO
 
*/

public class UsertableDAO extends BaseHibernateDAO ...{

 
private static final Log log = LogFactory.getLog(UsertableDAO.class);

 
public static final String NAME = "name";

 
public static final String AGE = "age";
/** *//**
 * save()方法提供了向数据库中添加数据的功能,但只能添加,这个DAO没有生成Update()的方法
 * 但你可以简单的八save()方法改称具有Update功能:将getSession().save
 * (transientInstance);这句改成
 * getSession().merge(transientInstance);或者getSession().saveOrUpdate
 *  (transientInstance);
 
*/

 
public void save(Usertable transientInstance) ...{
  log.debug(
"saving Usertable instance");
  
try ...{
   getSession().save(transientInstance);
   log.debug(
"save successful");
  }
 catch (RuntimeException re) ...{
   log.error(
"save failed", re);
   
throw re;
  }

 }

/** *//**
 * delete()方法看名知意是用来删除的.
 
*/

 
public void delete(Usertable persistentInstance) ...{
  log.debug(
"deleting Usertable instance");
  
try ...{
   getSession().delete(persistentInstance);
   log.debug(
"delete successful");
  }
 catch (RuntimeException re) ...{
   log.error(
"delete failed", re);
   
throw re;
  }

 }

/** *//**
 * findById()方法实现了按ID查询数据.
 
*/

 
public Usertable findById(java.lang.Integer id) ...{
  log.debug(
"getting Usertable instance with id: " + id);
  
try ...{
   Usertable instance 
= (Usertable) getSession().get("dao.Usertable",
     id);
   
return instance;
  }
 catch (RuntimeException re) ...{
   log.error(
"get failed", re);
   
throw re;
  }

 }

/** *//**
 * findByExample()方法实现的功能相当于"select * from Usertable"实现的功能就是查询所有 
 * 数据.
 
*/

 
public List findByExample(Usertable instance) ...{
  log.debug(
"finding Usertable instance by example");
  
try ...{
   List results 
= getSession().createCriteria("dao.Usertable").add(
     Example.create(instance)).list();
   log.debug(
"find by example successful, result size: "
     
+ results.size());
   
return results;
  }
 catch (RuntimeException re) ...{
   log.error(
"find by example failed", re);
   
throw re;
  }

 }

/** *//**
 * findByProperty()方法用来灵活的提供一种按条件查询的方法,你可以自己定义要按什么样的方
 * 式查询.
 
*/

 
public List findByProperty(String propertyName, Object value) ...{
  log.debug(
"finding Usertable instance with property: " + propertyName
    
+ ", value: " + value);
  
try ...{
   String queryString 
= "from Usertable as model where model."
     
+ propertyName + "= ?";
   Query queryObject 
= getSession().createQuery(queryString);
   queryObject.setParameter(
0, value);
   
return queryObject.list();
  }
 catch (RuntimeException re) ...{
   log.error(
"find by property name failed", re);
   
throw re;
  }

 }

/** *//**
 * findByName()和findByAge()方法就是调用了findByProperty()方法的实现按名字和年龄查询
 * 的功能
 
*/

 
public List findByName(Object name) ...{
  
return findByProperty(NAME, name);
 }


 
public List findByAge(Object age) ...{
  
return findByProperty(AGE, age);
 }

}


关于merge()  attachDirty() attachClean()三种方法下面做一个简单的介绍

/** *//** 
 * 将传入的detached状态的对象的属性复制到持久化对象中,并返回该持久化对象 
 * 如果该session中没有关联的持久化对象,加载一个,如果传入对象未保存,保存一个副本并作为持久对象返回,传入对象依然保持detached状态。 
 * 
@see com.CodeDepts 
 
*/
 
 
public CodeDepts merge(CodeDepts detachedInstance) ...
 log.debug(
"merging CodeDepts instance"); 
 
try ...
 CodeDepts result 
= (CodeDepts) getSession() 
 .merge(detachedInstance); 
 log.debug(
"merge successful"); 
 
return result; 
 }
 catch (RuntimeException re) ...
 log.error(
"merge failed", re); 
 
throw re; 
 }
 
 }
 

 
/** *//** 
 * 将传入的对象持久化并保存。 
 * 如果对象未保存(Transient状态),调用save方法保存。如果对象已保存(Detached状态),调用update方法将对象与Session重新关联。 

 * 
@see com.CodeDepts 
 
*/
 

分享到:
评论
1 楼 chaodilei 2012-07-04  
请问是用哪种插件自动生成的DAO,我用hibernatesync好像生成不了dao,只能生成javabean

相关推荐

Global site tag (gtag.js) - Google Analytics