Hibernate

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 17:22:44940browse

第一种是使用高级查询DetachedCriteria实现,代码如下: String alias = user_; //查詢時的table別名DetachedCriteria dc = De

第一种是使用高级查询DetachedCriteria实现,代码如下:

String alias = "user_"; //查詢時的table別名
DetachedCriteria dc = DetachedCriteria.forClass(User.class,alias);
ProjectionList pList = Projections.projectionList();
pList.add(Projections.property(alias + "." + "id").as("id"));
pList.add(Projections.property(alias + "." + "name").as("name"));
pList.add(Projections.property(alias + "." + "age").as("age"));
pList.add(Projections.property(alias + "." + "sex").as("sex"));
dc.setProjection(pList);
dc.setResultTransformer(Transformers.aliasToBean(User.class));
resultList = memberService.findByDetached(dc).size(); 

通過HQL语句查询

String hql = "select new Link(id,name) from Link";     
 
Query query = session.createQuery(hql); 
 
//默认查询出来的list里存放的是一个Object对象,,但是在这里list里存放的不再是默认的Object对象了,而是Link对象了 
 
List links = query.list(); 
 
for(Link link : links){ 
 
    String id = link.getId(); 
 
    String name = link.getName(); 
 
    System.out.println(id + " : " + name); 
 

第三种方式是通过HQL语句实现,类似SQL,方法如下:

String hql = "select id,name from Link";     
 
Query query = session.createQuery(hql); 
 
//默认查询出来的list里存放的是一个Object数组,还需要转换成对应的javaBean。 
 
List links = query.list(); 
 
for(Object[] link : links){ 
 
    String id = link[0]; 
 
    String name = link[1]; 
 
    System.out.println(id + " : " + name); 
 

linux

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn