班级: 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 clazzs = (List)session.createSQLQuery(sql).list();
sql = "select c.* from clazz c,student s where s.class_id = c.id";
List clazzs = (List)session.createSQLQuery(sql).list();
特别注意:返回结果是Object[]组成的List,通过Object下标访问属性
使用场合:非基于对象的查询,复杂查询
1.2 简单查询,返回持久化对象
sql = "select c.* from Clazz c , student s where s.class_id = c.id ";
特别注意: 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 clazzs = (List) session().createSQLQuery(sql)
String sql = "select {c.*}, {s.*} from Clazz c , student s where s.class_id = c.id
List clazzs = (List) session().createSQLQuery(sql)
.addEntity("c", Clazz.class)
.addEntity("s", Student.class).list();
view plaincopy to clipboardprint?
特别注意:
1. addEntity方法指定了当前返回实体的别名,而且在SQL语句里使用{c.*}这样的语句,
它的意思是向别名为c的这个对象注入所有属性值.
特别注意:
1. addEntity方法指定了当前返回实体的别名,而且在SQL语句里使用{c.*}这样的语句,
它的意思是向别名为c的这个对象注入所有属性值.
view plaincopy to clipboardprint?
2. 这个查询会返回一个Object的List,每个Object包含两个对象一个Clazz,一个Student
view plaincopy to clipboardprint?
3. {}占位符里是对象的属性,而非列名,如{s.class},而非{s.class_id}
view plaincopy to clipboardprint?
4. 必须注入对象的每个属性值,你可以使用{c.*},或者手工写全所有,但必须是所有属性。(这个太让我郁闷了。
view plaincopy to clipboardprint?
使用场合:基于对象的查询,复杂查询
1.4 多表查询,返回关联实体
view plaincopy to clipboardprint?
String sql = "select {c.*}, {s.*} from Clazz c , student s where s.class_id = c.id
List clazzs = (List) session().createSQLQuery(sql)
String sql = "select {c.*}, {s.*} from Clazz c , student s where s.class_id = c.id
List clazzs = (List) session().createSQLQuery(sql)
.addEntity("c", Clazz.class)
.addJoin("s", "c.students").list();
view plaincopy to clipboardprint?
特别注意: 这块有些问题,明明应该返回Clazz的List的,但确返回包含Clazz和Student的Object的List, faint
使用场合:基于对象的查询,复杂查询
2. 查询托管对象
view plaincopy to clipboardprint?
String sql = "select c.id,
List clazzs = (List) session.createSQLQuery(sql)
String sql = "select c.id,
List clazzs = (List) session.createSQLQuery(sql)
.addScalar("id", Hibernate.LONG)
.addScalar("name", Hibernate.STRING)
.setResultTransformer(Transformers.aliasToBean(Clazz.class)).list();
特别注意:
1. 如果不设置addScalar方法可能会报转型错误的异常.
2.托管查询可以使用投影,只查询指定的列属性
==================================================华丽分割====以上为转载 http://blog.sina.com.cn/s/blog_5fcdcbf70100lfuq.html=====- Query
query = this.getSession().createSQLQuery("select {em.*},{pc.*},{dept.*},{p.*} " + -
"from t_postcount pc ,t_post p ,t_employees em ,t_department dept " + -
"where pc.post = p.id and pc.epid = em.id and p.pid = dept.id " + -
"and em.id = "+employeesId+" order by dept.id,pc.date desc ") -
.addEntity("em",Employees.class) -
.addEntity("pc",PostCount.class) -
.addEntity("dept",Department.class) -
.addEntity("p",Post.class); -
List -
List list = new ArrayList(); -
for(int i=0;i -
PostCountBean pcb = new PostCountBean(); -
pcb.setName(((Employees)l.get(i)[0]).getName()); -
pcb.setDate(( (PostCount)l.get(i)[1]).getDate()); -
pcb.setDname(( (Department)l.get(i)[2]).getName()); -
pcb.setPname(( (Post)l.get(i)[3]).getName()); -
pcb.setPostNum(( (PostCount)l.get(i)[1]).getPostNum()); -
System.out.print (((Employees)l.get(i)[0]).getName()); -
System.out.print (( (PostCount)l.get(i)[1]).getDate()); -
System.out.print (( (Department)l.get(i)[2]).getName()); -
System.out.println(( (Post)l.get(i)[3]).getName()); -
list.add(pcb); -
}