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

推荐订阅源

N
Netflix TechBlog - Medium
罗磊的独立博客
H
Help Net Security
I
Intezer
G
Google Developers Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Troy Hunt's Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
J
Java Code Geeks
S
Security Affairs
T
The Blog of Author Tim Ferriss
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Docker
The GitHub Blog
The GitHub Blog
F
Full Disclosure
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
腾讯CDC
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
T
Threatpost
D
DataBreaches.Net
Recent Announcements
Recent Announcements
博客园 - 三生石上(FineUI控件)
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
S
Securelist
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
P
Proofpoint News Feed
Project Zero
Project Zero
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 叶小钗

博客园 - xiaolei1982

asp.net的ajax的实现 spring:bean生命周期(转贴) 在myeclipse下整合spring和hibernate(转贴) Struts+Spring+Hibernate练习(转贴) spring:bean注入(转贴) - xiaolei1982 - 博客园 ASP.NET ViewState 实现分析(转贴) 自定义控件源码(转贴) ASP.NET服务器控件的开发(4)(转贴) ASP.NET服务器控件的开发(3)(转贴) ASP.NET服务器控件的开发(2) (转贴) ASP.NET服务器控件的开发(1)(转贴) Web 页本是无状态而断续的(转贴) asp.net控件开发基础(3)(转贴) asp.net控件开发基础(6)(转贴) asp.net控件开发基础(5)(转贴) asp.net控件开发基础(4)(转贴) asp.net控件开发基础(2)(转贴) asp.net控件开发基础(1)(转贴) C#中使用反射显示程序集的所有类型和属性(转贴)
解析eclipse下生成Hibernate DAO中的几个方法(转贴)
xiaolei1982 · 2008-06-04 · via 博客园 - xiaolei1982

Posted on 2008-06-04 23:20  xiaolei1982  阅读(1172)  评论()    收藏  举报

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

}


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