详细概念在ppt上:
1.HQL
(1)Query接口
- Object uniqueResult(),返回匹配条件的唯一实例,若无,则为null。
2.QBC(Criteria接口、Criterion接口、Expression类<所以的设置条件都在这个方法内>)
(1)session内创建
- Criteria createCriteria(class class) 持久化类名
(2)Criteria接口
- Object uniqueResult()
- List list()
- Criterion add(Criterion criterion) 对查询结果增加一些限制
(3)Criterion接口,由Restictions(实现类),内部都是静态方法
(4)Criteria
- addOrder(Order order) 排序
(5)Static Order asc(String name)
- Static Order desc(String propertyName)
(6)Query
- List list() 若每行包含多个结果,则结果返回object[] ,查询结果为list。
(7)查询部分属性:
public class Student extends Person { private String id; private String name; private Set course = new HashSet(); private int age; private String cardId;
public class Course { private String id; private String name; private Set students = new HashSet();
public class Team { private String id; private String teamName; private Set students = new HashSet();
- session.creatQuery("select s.name, s.age from Student as s") -----查询出游离的数据
List list =query.list();
for(int i=0;i<list.size();i++)
{
Object[] obj =(Object[])list.get(i);
obj[0] 为name ;obj[1]为age
}
- Query query = session.creatQuery("select new Student(s.name,s.age) from Student s");返回的离散的值,当做对象来看;
不过必须在Student类中,加入包括这两个参数的构造函数。
Student student =(Student)list.get(i);
student.getName();
student.getAge();
(7)连接查询(内连接、左外连接、右外连接)
- Query query =session.creatQuery("form Team as t inner join t.students"); inner可以省略
返回两张表的并集,返回一个object[],且object[0]为第一张,object[1]为第二张。
SQL:select *from team join student on team.id = student.team_id;
- hibernate.cfg.xml
<property name="format_sql">true</property> 格式化查询
- select * from team left outer join student on team.id=student.team_id
左外连接(以左表为参照物,即左表的内容是存在的),右外连接相反
- select * from team right outer join student on team.id=student.team_id