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

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;
   }

}

**
* 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);
}

}

/**
* 将传入的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
*/

public void attachDirty(CodeDepts instance) ...{
log.debug(
"attaching dirty CodeDepts instance");
try ...{
getSession().saveOrUpdate(instance);
log.debug(
"attach successful");
}
catch (RuntimeException re) ...{
log.error(
"attach failed", re);
throw re;
}

}

  
  

/** *//**
* 将传入的对象状态设置为Transient状态
*
@see com.CodeDepts
*/

public void attachClean(CodeDepts instance) ...{
log.debug(
"attaching clean CodeDepts instance");
try ...{
getSession().lock(instance, LockMode.NONE);
log.debug(
"attach successful");
}
catch (RuntimeException re) ...{
log.error(
"attach failed", re);
throw re;
}

}

你可能感兴趣的:(Hibernate)