hibernate SQLQuery实践指南

准备:
两个表(实体), 1对多双向关联:

班级: Clazz (id, name)

学生: Student(id, name , class_id)



1. 查询持久化对象  
1.1 简单查询,返回数据库结构
view plaincopy to clipboardprint?
sql = "select c.* from clazz c,student s where s.class_id = c.id";     
 
List<Clazz> clazzs = (List<Clazz>)session.createSQLQuery(sql).list(); 

sql = "select c.* from clazz c,student s where s.class_id = c.id";  

List<Clazz> clazzs = (List<Clazz>)session.createSQLQuery(sql).list();特别注意:返回结果是Object[]组成的List,通过Object下标访问属性

使用场合:非基于对象的查询,复杂查询

1.2 简单查询,返回持久化对象
  sql = "select c.* from Clazz c , student s where s.class_id = c.id ";    List<Clazz> clazzs = (List<Clazz>) session.createSQLQuery(sql).addEntity(Clazz.class).list();

特别注意: 1.addEntity方法指定返回包含Clazz对象的List 2. 查询出来的持久态对象,clazz对象的studets属性是各代理对象,访问的时候才会去查询DB 使用场合:基于对象的查询,但对象简单查询

1.3 多表查询,返回多个实体
view plaincopy to clipboardprint?
String sql = "select {c.*}, {s.*} from Clazz c , student s where s.class_id = c.id   ";  
 
List<Clazz> clazzs = (List<Clazz>) session().createSQLQuery(sql)  
 
                                 .addEntity("c", Clazz.class)  
 
                                 .addEntity("s", Student.class).list(); 

String sql = "select {c.*}, {s.*} from Clazz c , student s where s.class_id = c.id   ";

List<Clazz> clazzs = (List<Clazz>) session().createSQLQuery(sql)

                                 .addEntity("c", Clazz.class)

                                 .addEntity("s", Student.class).list();view plaincopy to clipboardprint?
特别注意:   
1. addEntity方法指定了当前返回实体的别名,而且在SQL语句里使用{c.*}这样的语句,  
它的意思是向别名为c的这个对象注入所有属性值. 

2. 这个查询会返回一个Object的List,每个Object包含两个对象一个Clazz,一个

3. {}占位符里是对象的属性,而非列名,如{s.class},而非{s.class_id}

4. 必须注入对象的每个属性值,你可以使用{c.*},或者手工写全所有,但必须是所有属性。(这个太让我郁闷了。

使用场合:基于对象的查询,复杂查询 

1.4  多表查询,返回关联实体

String sql = "select {c.*}, {s.*} from Clazz c , student s where s.class_id = c.id   ";  
 
List<Clazz> clazzs = (List<Clazz>) session().createSQLQuery(sql)  
 
        .addEntity("c", Clazz.class)  
 
        .addJoin("s", "c.students").list(); 

String sql = "select {c.*}, {s.*} from Clazz c , student s where s.class_id = c.id   ";

List<Clazz> clazzs = (List<Clazz>) session().createSQLQuery(sql)

  .addEntity("c", Clazz.class)

  .addJoin("s", "c.students").list();view plaincopy to clipboardprint?
<P>特别注意: 这块有些问题,明明应该返回Clazz的List的,但确返回包含Clazz和Student的Object的List, faint</P><P>使用场合:基于对象的查询,复杂查询</P> 

特别注意: 这块有些问题,明明应该返回Clazz的List的,但确返回包含Clazz和Student的Object的List, faint使用场合:基于对象的查询,复杂查询2. 查询托管对象

view plaincopy to clipboardprint?
String sql = "select c.id, c.name from Clazz c , student s where s.class_id = c.id   ";     
 
List<Clazz> clazzs = (List<Clazz>) session.createSQLQuery(sql)        
 
                                     .addScalar("id", Hibernate.LONG)  
 
                                     .addScalar("name", Hibernate.STRING)       
 
                                     .setResultTransformer(Transformers.aliasToBean(Clazz.class)).list(); 

String sql = "select c.id, c.name from Clazz c , student s where s.class_id = c.id   ";  

List<Clazz> clazzs = (List<Clazz>) session.createSQLQuery(sql)     

                                     .addScalar("id", Hibernate.LONG)

                                     .addScalar("name", Hibernate.STRING)    

                                     .setResultTransformer(Transformers.aliasToBean(Clazz.class)).list();

特别注意:

1. 如果不设置addScalar方法可能会报转型错误的异常.

2.托管查询可以使用投影,只查询指定的列属性



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/blackpark/archive/2008/07/05/2613754.aspx#

你可能感兴趣的:(sql,C++,c,Hibernate,C#)