Hibernate检索对象的方式
- HQL检索方式
- QBC检索方式
1、Hibernate提供的检索对象的方式
- 导航图对象检索方式
根据已经加载的对象,导航到其他对象。例如,对于已经加载的Customer对象,调用它的getOrders().iterator()方法就可以导航到所有关联的Order对象,假如在关联级别使用了延迟家在检索策略,那么首次执行此方法时,hibernate会从数据库中加载关联的Order对象,否则就从缓存中取得Order对象。
- OID检索方式
按照对象的OID来检索对象。Session的get()和load()方法提供了这种功能。如果在应用程序中事先知道了OID,就可以使用这种检索对象的方式。
- HQL检索方式
HIbernate提供了Query接口,它是Hibernate提供的专门的HQL查询接口,能够执行各种复杂的HQL查询语句。
- QBC检索方式
使用QBC(Query By Criteria)API来检索对象。这种API封装了基于字符串形式的查询语句,提供了更加面向对象的接口。
2、HQL检索方式
HQL(Hibernate Query Language)是面向对象的查询语言,他和SQL查询语言有些相似。在Hibernate提供的各种检索方式中,HQL是使用最广的一种检索方式。它具有以下功能:
- 在查询语句中设定各种查询条件
- 支持投影查询,即仅检索出对象的部分属性
- 支持分页查询
- 支持连接查询
- 支持分组查询,允许使用having和group by关键字
- 提供内置聚集函数,如sum()、min()、和max()
- 支持子查询,即嵌入式查询
- 支持动态绑定参数
HQL检索步骤:
//创建一个Query
Query query = session.createQuery("from Customer as c where " + "c.name =: customerName" + "and c.age =: customerAge");
//这里c.name =: customerName,c.name是指出Customer的属性name,=:是一个动态赋值的符号,后面的customerName是任意起名的,是我们要动态付值得一个临时变量。c.age =: customerAge同理
//动态绑定参数
query.setString("customerName","tom");
query.setInteger("customerAge",20);
//执行查询语句,返回查询结果
List result = query.list();
Query提供了大量的setXXX方法,来为其提供设定参数。
(1)通过Session的createQuery()方法创建一个Query对象,它包含一个HQL查询语句。HQL查询语句可以包含命名参数,如”customerName“和”customerAge“都是命名参数。
(2)动态绑定参数。Query接口提供了各种类型的命名参数赋值的方法,例如setString()方法用于为字符串类型的customerName命名参数赋值。
(3)调用Query的list()方法执行查询语句。该方法返回List类型的查询结果,在List集合中存放了符合查询条件的持久化对象。
对于Query支持方法链编程风格
List result = session.greateQuery("...").setString("customerName","Tom").setInteger("customerAge",21).list();
方法链编程风格能使程序代码更加简洁
3、QBC检索方式
采用HQL检索方式时,在应用程序中需要定义基于字符串形式的HQL查询语句。
QBC API提供了检索对象的另一种方式,它主要由Criteria接口、Criterion接口和Expression类组成,它支持在运行时动态生成查询语句。
Criteria是由Session创建的,Session的createCriteria()创建一个Criteria对象。Criterion是增加约束条件的,具体实现类Restrictions
//创建一个Criteria对象
Criteria criteria = session.createCriteria(Customer.clss);
//设定查询条件,然后把查询条件加入到Criteria中
Criterion criterion1 = Expression.like(”name“,”T%“); //相当于where name like ‘T%’
Criterion criterion2 = Expression.eq("age",new Integer(21));
criteria = criteria.add(criterion1);
criteria = criteria.add(criterion2);
//执行查询语句,返回查询结果
List result = criteria.list();
使用QBC检索的步骤
-(1)调用Session的createCriteria()方法创建一个Criteria对象。
-(2)设定查询条件。Expression类提供了一系列用于设定查询条件的静态方法,这些静态方法都返回Criterion实例,每个Criterion实例代表一个查询条件。Criteria的add()方法用于加入查询条件。
-(3)调用Criteria的list()方法执行查询语句。该方法返回List类型的查询结果,在List集合中存放了符合查询条件的持久化对象。对于以上程序代码,当运行Criteria的list()方法时,Hibernate执行的SQL查询语句为:
select * from customers where name like 'T%' and age = 21;
方法链编程风格
List result = session.createCriteria(Customer.class).add(Expression.like("name","T%")).add(Expression.eq("age",new Integer(21))).list();
4、分页查询
Query和Criteria接口都提供了用于分页显示查询结果的方法:
- setFirstResult(int firstResult):设定从哪一个对象开始检索,参数firstResult表示这个对象在查询结果中的索引位置,索引位置的起始值为0.默认情况下,Query和Criteria接口从查询结果中的第一个对象,也就是索引位置为0的对象开始检索。
- seMaxResult(int maxResults):设定一次最多检索出的对象数目。默认情况下,Query和Criteria接口检索出查询结果中所有的对象。
分页查询:
//采用HQL检索方式
Query query = session.createQuery("from Customer c order by c.name asc");
query.setFirstResults(0);
query.setMaxResults(10);
List result = query.list(); |
//采用QBC检索方式
Criteria criteria=session.createCritia(Customer.class);
criteria.addOrder(Order.asc("name"));
criteria.setFirstResults(0);
criteria.setMaxResults(10);
List result = criteria.list(); |
5、查询检索方式举例:
使用Team、Student和Course三个类:
- import java.util.HashSet;
- import java.util.Set;
-
- public class Team
- {
- private String id;
-
- private String teamName;
-
- private Set students = new HashSet();
-
- public String getId()
- {
- return id;
- }
-
- public void setId(String id)
- {
- this.id = id;
- }
-
-
- public String getTeamName()
- {
- return teamName;
- }
-
- public void setTeamName(String teamName)
- {
- this.teamName = teamName;
- }
-
- public Set getStudents()
- {
- return students;
- }
-
- public void setStudents(Set students)
- {
- this.students = students;
- }
-
- }
- import java.util.Set;
-
- public class Student
- {
- private String id;
-
- private String cardId;
-
- private String name;
-
- private int age;
-
- public String getCardId()
- {
- return cardId;
- }
-
- public void setCardId(String cardId)
- {
- this.cardId = cardId;
- }
-
- public int getAge()
- {
- return age;
- }
-
- public void setAge(int age)
- {
- this.age = age;
- }
-
- private Set<Course> courses;
-
- public String getId()
- {
- return id;
- }
-
- public void setId(String id)
- {
- this.id = id;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-
- public Set<Course> getCourses()
- {
- return courses;
- }
-
- public void setCourses(Set<Course> courses)
- {
- this.courses = courses;
- }
-
- }
- import java.util.Set;
-
- public class Course
- {
- private String id;
-
- private String name;
-
- private Set<Student> students;
-
- public String getId()
- {
- return id;
- }
-
- public void setId(String id)
- {
- this.id = id;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-
- public Set<Student> getStudents()
- {
- return students;
- }
-
- public void setStudents(Set<Student> students)
- {
- this.students = students;
- }
-
- }
对应的映射文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate./hibernate-mapping-3.0.dtd">
-
- <hibernate-mapping>
-
- <class name="com.cdtax.hibernate.Team" table="team">
-
- <id name="id" column="id" type="string">
- <generator class="uuid">
- </generator>
- </id>
-
- <property name="teamName" column="teamname" type="string"></property>
-
- <set name="students" lazy="false" cascade="all" inverse="true" fetch="select">
- <key column="team_id"></key>
- <one-to-many class="com.cdtax.hibernate.Student"/>
- </set>
-
- </class>
- </hibernate-mapping>
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate./hibernate-mapping-3.0.dtd">
-
- <hibernate-mapping>
-
- <class name="com.cdtax.hibernate.Student" table="student">
-
- <id name="id" column="id" type="string">
- <generator class="uuid"></generator>
- </id>
-
- <property name="name" column="name" type="string"></property>
- <property name="cardId" column="cardid" type="string"></property>
- <property name="age" column="age" type="int"></property>
-
-
- <set name="courses" table="student_course" cascade="save-update">
- <!-- cascade不能使用all -->
- <key column="student_id"></key>
- <many-to-many class="com.cdtax.hibernate.Course" column="course_id"></many-to-many>
- </set>
- </class>
- </hibernate-mapping>
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate./hibernate-mapping-3.0.dtd">
-
- <hibernate-mapping>
-
- <class name="com.cdtax.hibernate.Course" table="course">
-
- <id name="id" column="id" type="string">
- <generator class="uuid"></generator>
- </id>
-
- <property name="name" column="name" type="string"></property>
-
- <set name="students" table="student_course" cascade="save-update" inverse="true">
- <!-- cascade不能使用all -->
- <key column="course_id"></key>
- <many-to-many class="com.cdtax.hibernate.Student" column="student_id"></many-to-many>
- </set>
- </class>
- </hibernate-mapping>
产生数据库表:
create table course (id varchar(255) not null, name varchar(255), primary key (id))
create table student (id varchar(255) not null, name varchar(255), cardid varchar(255), age integer, team_id varchar(255), primary key (id))
create table student_course (student_id varchar(255) not null, course_id varchar(255) not null, primary key (student_id, course_id))
create table team (id varchar(255) not null, teamname varchar(255), primary key (id))
alter table student add index FK8FFE823BB04F9E7 (team_id), add constraint FK8FFE823BB04F9E7 foreign key (team_id) references team (id)
alter table student_course add index FKB0A3729F45A3E36D (student_id), add constraint FKB0A3729F45A3E36D foreign key (student_id) references student (id)
alter table student_course add index FKB0A3729FA31F19E7 (course_id), add constraint FKB0A3729FA31F19E7 foreign key (course_id) references course (id)
插入一些测试数据:
USE hibernate;
insert into `course`(`id`,`name`) values ('402881c04223208801422320897a0031','yy');
insert into `course`(`id`,`name`) values ('402881c04223208801422320897a0032','sx');
insert into `course`(`id`,`name`) values ('402881c04223208801422320897a0033','yw');
insert into `student`(`id`,`name`,`cardid`,`age`,`team_id`) values ('402881c04223208801422320897a0011','zhangsan','402881c04223208801422320897a0021',20,'402881c04223208801422320897a0001');
insert into `student`(`id`,`name`,`cardid`,`age`,`team_id`) values ('402881c04223208801422320897a0012','lisi','402881c04223208801422320897a0022',7,'402881c04223208801422320897a0001');
insert into `student`(`id`,`name`,`cardid`,`age`,`team_id`) values ('402881c04223208801422320897a0013','wangwu','402881c04223208801422320897a0023',45,'402881c04223208801422320897a0002');
insert into `student`(`id`,`name`,`cardid`,`age`,`team_id`) values ('402881c04223208801422320897a0014','zhaoliu','402881c04223208801422320897a0024',34,'402881c04223208801422320897a0002');
insert into `student`(`id`,`name`,`cardid`,`age`,`team_id`) values ('402881c04223208801422320897a0015','kklk','402881c04223208801422320897a0025',23,'402881c04223208801422320897a0003');
insert into `team`(`id`,`teamname`) values ('402881c04223208801422320897a0001','team1');
insert into `team`(`id`,`teamname`) values ('402881c04223208801422320897a0002','team2');
insert into `team`(`id`,`teamname`) values ('402881c04223208801422320897a0003','team3');
insert into `student_course`(`student_id`,`course_id`) values ('402881c04223208801422320897a0011','402881c04223208801422320897a0031');
insert into `student_course`(`student_id`,`course_id`) values ('402881c04223208801422320897a0011','402881c04223208801422320897a0032');
insert into `student_course`(`student_id`,`course_id`) values ('402881c04223208801422320897a0012','402881c04223208801422320897a0033');
insert into `student_course`(`student_id`,`course_id`) values ('402881c04223208801422320897a0013','402881c04223208801422320897a0032');
使用HQL查询学生的信息,如果是查询学生的所有信息,就用from Student这种写法,但是这里我们只是查询学生的name和age,写法要注意如下:
- import java.util.List;
-
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
-
- public class HibernateTest
- {
- private static SessionFactory sessionFactory;
-
- static
- {
- try
- {
- sessionFactory = new Configuration().configure().buildSessionFactory();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
-
- public static void main(String[] args)
- {
-
- Session session = sessionFactory.openSession();
- Transaction tx = null;
-
- try
- {
- tx = session.beginTransaction();
-
- Query query = session.createQuery("<span style="color:#ff0000;">select s.name,s.age from Student</span> s");
- <span style="color:#ff0000;">//对于以前查询学生的所有信息,就用from Student这种写法,现在是查询学生的name和age,写法就如上
- //对于查询出来的结果,如果是from Student,那么查询出来的就是Student对象,现在只是name和age,是游离的,跟Student看不出有任何关系
-
- List list = query.list();
- //对于list中的元素,如果是from Student方式,那么就是Student实例
- //但是现在就不是了,查看Query的list()方法说明:Return the query results as a List.If the query contains multiple results pre row
- //the results are returned in an instance of Object[].如果query每一行包含多个结果,那么结果就作为一个Objec[]数组返回。
- //对于我们这个程序list中的元素就是一个Object[]数组,其中Object[0]就是name,Object[1]就是age
- //所以我们要进行类型转换 </span>
- for(int i = 0; i < list.size(); i++)
- {
- <span style="color:#ff0000;">Object[] obj = (Object[])list.get(i);</span>
-
- System.out.println(<span style="color:#ff0000;">obj[0] + ", " + obj[1]</span>);
- }
-
- tx.commit();
- }
- catch(Exception ex)
- {
- if(null != tx)
- {
- tx.rollback();
- }
- ex.printStackTrace();
- }
- finally
- {
- session.close();
- }
- }
- }
看下面的对应关系图:
执行的结果:
Hibernate: select student0_.name as col_0_0_, student0_.age as col_1_0_ from student student0_
zhangsan, 20
lisi, 7
wangwu, 45
zhaoliu, 34
kklk, 23
Hibernate也提供了直接查询部分属性,而且返回也是相应类的查询方式,如下:
- try
- {
- tx = session.beginTransaction();
-
-
-
- Query query = session.createQuery("<span style="color:#ff0000;">select <strong><em>new Student(s.name,s.age)</em></strong> from Studnet s</span>");
-
- List list = query.list();
-
- for(int i = 0; i < list.size(); i++)
- {
- <span style="color:#ff0000;">Student student = (Student)list.get(i)</span>;
-
- System.out.println(student.getName() + ", " + student.getAge());
- }
- tx.commit();
- }
这时list()返回的List元素就是Student。执行:结果出现异常了:
org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.cdtax.hibernate.Student] [select new Student(s.name,s.age) from com.cdtax.hibernate.Student s]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:261)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651)
at com.cdtax.hibernate.HibernateTest.main(HibernateTest.java:54)
什么原因呢????原来Hibernate使用构造函数来生成list的元素,就是Student,而Student没有具有两个参数(String name,int age)的构造函数,所以修改一下Student类,增加相应的构造函数,一定要注意参数的顺序。
- import java.util.Set;
-
- public class Student
- {
- private String id;
-
- private String cardId;
-
- private String name;
-
- private int age;
-
- <span style="color:#ff0000;">public Student()
- {
-
- }
-
- public Student(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
- </span>
- public String getCardId()
- {
- return cardId;
- }
-
- public void setCardId(String cardId)
- {
- this.cardId = cardId;
- }
-
- public int getAge()
- {
- return age;
- }
-
- public void setAge(int age)
- {
- this.age = age;
- }
-
- private Set<Course> courses;
-
- public String getId()
- {
- return id;
- }
-
- public void setId(String id)
- {
- this.id = id;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-
- public Set<Course> getCourses()
- {
- return courses;
- }
-
- public void setCourses(Set<Course> courses)
- {
- this.courses = courses;
- }
-
- }
在执行结果正确显示:
Hibernate: select student0_.name as col_0_0_, student0_.age as col_1_0_ from student student0_
zhangsan, 20
lisi, 7
wangwu, 45
zhaoliu, 34
kklk, 23
我们在打印的时候增加一行:System.out.println(student.getCardId());结果:
Hibernate: select student0_.name as col_0_0_, student0_.age as col_1_0_ from student student0_
zhangsan, 20
null
lisi, 7
null
wangwu, 45
null
zhaoliu, 34
null
kklk, 23
null
cardid为null,因为我们没有查此字段。
6、连接操作
如下的查询:
- Query query = session.createQuery("from Team t join t.students");
-
- List list = query.list();
产生的sql语句如下:
Hibernate:
select
team0_.id as id3_0_,
students1_.id as id0_1_,
team0_.teamname as teamname3_0_,
students1_.name as name0_1_,
students1_.cardid as cardid0_1_,
students1_.age as age0_1_
from
team team0_
inner join
student students1_
on team0_.id=students1_.team_id
Hibernate:
select
students0_.team_id as team5_1_,
students0_.id as id1_,
students0_.id as id0_0_,
students0_.name as name0_0_,
students0_.cardid as cardid0_0_,
students0_.age as age0_0_
from
student students0_
where
students0_.team_id=?
Hibernate:
select
students0_.team_id as team5_1_,
students0_.id as id1_,
students0_.id as id0_0_,
students0_.name as name0_0_,
students0_.cardid as cardid0_0_,
students0_.age as age0_0_
from
student students0_
where
students0_.team_id=?
Hibernate:
select
students0_.team_id as team5_1_,
students0_.id as id1_,
students0_.id as id0_0_,
students0_.name as name0_0_,
students0_.cardid as cardid0_0_,
students0_.age as age0_0_
from
student students0_
where
students0_.team_id=?
简单地写join就是内连接:inner join
inner join student students1_ on team0_.id=students1_.team_id
等价于:select * from team team0_ ,student students1_ where team0_.id=students1_.team_id
左外连接,以连接左面的表为准(为参照表),查询结果一定包含左边表的全部和右边表匹配的记录。left outer join
同理,右外连接以右面的表为参照。
对于上面的查询,查询的是team和student中的所有字段,所以list结果是其元素是一个Object的数组,数组中是Team和Student对象。
- Query query = session.createQuery("from Team t join t.students");
-
- List list = query.list();
-
- for(int i = 0; i < list.size(); i++)
- {
- <span style="color:#ff0000;">Object[] obj = (Object[])list.get(i);
-
- Team team = (Team)obj[0];
- Student student = (Student)obj[1];</span>
-
- System.out.println(team.getTeamName());
- System.out.println(student.getName() + ", " + student.getAge());
- System.out.println("====================");
- }
执行结果:
team1
zhangsan, 20
====================
team1
lisi, 7
====================
team2
wangwu, 45
====================
team2
zhaoliu, 34
====================
team3
kklk, 23
====================
7、将Team.hbm.xml中的set设为延迟加载,即lazy=”true“
修改测试程序:
- import java.util.List;
-
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
-
- public class HibernateTest
- {
- private static SessionFactory sessionFactory;
-
- static
- {
- try
- {
- sessionFactory = new Configuration().configure().buildSessionFactory();
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- }
-
- public static void main(String[] args)
- {
-
- Session session = sessionFactory.openSession();
- Transaction tx = null;
- Query query = null;
-
- List list = null;
- try
- {
- tx = session.beginTransaction();
-
-
- query = session.createQuery("from Team t join t.students");
-
- list = query.list();
-
- tx.commit();
- }
- catch(Exception ex)
- {
- if(null != tx)
- {
- tx.rollback();
- }
- ex.printStackTrace();
- }
- finally
- {
- session.close();
- }
-
- for(int i = 0; i < list.size(); i++)
- {
- Object[] obj = (Object[])list.get(i);
-
- Team team = (Team)obj[0];
- Student student = (Student)obj[1];
-
- System.out.println(team.getTeamName());
- System.out.println(student.getName() + ", " + student.getAge());
- System.out.println("====================");
- }
- }
- }
使用了join后,连同Student的信息就一并查询出来了,lazy延时加载就被覆盖了,变成了立即加载,所以在session关闭后,打印student也能出来结果,如果只查询team,而且又使用了延迟加载,那么在关系session后在打印student是打印不出来的。
8、使用实体命名参数查询
修改Student,增加一个Team成员变量:
- import java.util.Set;
-
- public class Student
- {
- private String id;
-
- private String cardId;
-
- private String name;
-
- private int age;
-
- private Team team;
-
- public Team getTeam()
- {
- return team;
- }
-
- public void setTeam(Team team)
- {
- this.team = team;
- }
-
- public Student()
- {
-
- }
-
- public Student(String name, int age)
- {
- this.name = name;
- this.age = age;
- }
-
- public String getCardId()
- {
- return cardId;
- }
-
- public void setCardId(String cardId)
- {
- this.cardId = cardId;
- }
-
- public int getAge()
- {
- return age;
- }
-
- public void setAge(int age)
- {
- this.age = age;
- }
-
- private Set<Course> courses;
-
- public String getId()
- {
- return id;
- }
-
- public void setId(String id)
- {
- this.id = id;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-
- public Set<Course> getCourses()
- {
- return courses;
- }
-
- public void setCourses(Set<Course> courses)
- {
- this.courses = courses;
- }
-
- }
修改HBM文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate./hibernate-mapping-3.0.dtd">
-
- <hibernate-mapping>
-
- <class name="com.cdtax.hibernate.Student" table="student">
-
- <id name="id" column="id" type="string">
- <generator class="uuid"></generator>
- </id>
-
- <property name="name" column="name" type="string"></property>
- <property name="cardId" column="cardid" type="string"></property>
- <property name="age" column="age" type="int"></property>
-
-
- <set name="courses" table="student_course" cascade="save-update">
- <!-- cascade不能使用all -->
- <key column="student_id"></key>
- <many-to-many class="com.cdtax.hibernate.Course" column="course_id"></many-to-many>
- </set>
-
- <span style="color:#ff6666;"><many-to-one name="team" class="com.cdtax.hibernate.Team" fetch="select" column="team_id"></many-to-one></span>
- </class>
- </hibernate-mapping>
增加了many-to-one
查询测试:
- try
- {
- tx = session.beginTransaction();
-
- Team team = (Team)session.get(Team.class, "402881c04223208801422320897a0002");
-
- Query query = session.createQuery("<span style="color:#ff0000;">from Student s where s.team = :team and s.age > 20</span>");
-
- query.<span style="color:#ff0000;">setParameter</span>("team", team,Hibernate.entity(Team.class));
-
- List<Student> list = query.list();
-
- System.out.println(list.size());
- tx.commit();
- }
这里要注意,使用命名参数=:与命名参数之间不能有空格=和:之间可以有空格,query的setParameter方法,第一个参数是命名参数,第二个是参数的值,第三个是type,是Hibernate要求的type值,这个值的获取是使用org.hibrenate.Hibernate类提供的静态方法entity()来获得的。
第二种设定实体查询的命名参数的方法
- try
- {
- tx = session.beginTransaction();
-
- Team team = (Team)session.get(Team.class, "402881c04223208801422320897a0002");
-
- Query query = session.createQuery("from Student s where s.team = :team and s.age > 20");
-
- // query.setParameter("team", team,Hibernate.entity(Team.class));
-
- <span style="color:#ff0000;">query.setEntity("team", team);</span>
-
- List<Student> list = query.list();
-
- System.out.println(list.size());
- tx.commit();
- }
query的setEntity()方法更简单。
session提供的过滤功能,createFilter(Object collection,String queryString)
- try
- {
- tx = session.beginTransaction();
-
- Team team = (Team)session.get(Team.class, "402881c04223208801422320897a0002");
-
- // Query query = session.createQuery("from Student s where s.team = :team and s.age > 20");
-
- // query.setParameter("team", team,Hibernate.entity(Team.class));
-
- // query.setEntity("team", team);
-
- <span style="color:#ff0000;">Query query = session.createFilter(team.getStudents(), "where age > 20");</span>
-
- List<Student> list = query.list();
-
- System.out.println(list.size());
- tx.commit();
- }
9、QBC查询
between:
- try
- {
- tx = session.beginTransaction();
-
- <span style="color:#ff0000;">Criteria criteria = session.createCriteria(Student.class)
- .add(Restrictions.between("age", new Integer(10), new Integer(30)));</span>
-
- List<Student> list = criteria.list();
-
- for(Student student : list)
- {
- System.out.println(student.getName());
- }
- tx.commit();
- }
like:
- try
- {
- tx = session.beginTransaction();
-
- // Criteria criteria = session.createCriteria(Student.class)
- // .add(Restrictions.between("age", new Integer(10), new Integer(30)));
-
- Criteria criteria = session.createCriteria(Student.class)
- .add(Restrictions.<span style="color:#ff0000;">like</span>("name", "k%"));
- List<Student> list = criteria.list();
-
- for(Student student : list)
- {
- System.out.println(student.getName());
- }
- tx.commit();
- }
in:
- try
- {
- tx = session.beginTransaction();
-
- // Criteria criteria = session.createCriteria(Student.class)
- // .add(Restrictions.between("age", new Integer(10), new Integer(30)));
-
- // Criteria criteria = session.createCriteria(Student.class)
- // .add(Restrictions.like("name", "k%"));
-
- String[] names = {"zhangsan","lisi","wangwu"};
-
- Criteria criteria = session.createCriteria(Student.class)
- .add(Restrictions.<span style="color:#ff0000;">in</span>("name",names));
-
- List<Student> list = criteria.list();
-
- for(Student student : list)
- {
- System.out.println(student.getName());
- }
- tx.commit();
- }
排序:addOrder
- try
- {
- tx = session.beginTransaction();
-
- // Criteria criteria = session.createCriteria(Student.class)
- // .add(Restrictions.between("age", new Integer(10), new Integer(30)));
-
- // Criteria criteria = session.createCriteria(Student.class)
- // .add(Restrictions.like("name", "k%"));
-
- // String[] names = {"zhangsan","lisi","wangwu"};
- //
- // Criteria criteria = session.createCriteria(Student.class)
- // .add(Restrictions.in("name",names));
-
- Criteria criteria = session.createCriteria(Student.class)
- .addOrder(Order.asc("age")).addOrder(Order.desc("cardId"));
-
- List<Student> list = criteria.list();
-
- for(Student student : list)
- {
- System.out.println(student.getName()+","+ student.getAge() + ","+ student.getCardId());
- System.out.println("-----------------");
- }
- tx.commit();
- }
执行结果:
Hibernate:
select
this_.id as id0_0_,
this_.name as name0_0_,
this_.cardid as cardid0_0_,
this_.age as age0_0_,
this_.team_id as team5_0_0_
from
student this_
order by
this_.age asc,
this_.cardid desc
lisi,7,402881c04223208801422320897a0022
-----------------
zhangsan,20,402881c04223208801422320897a0021
-----------------
kklk,23,402881c04223208801422320897a0025
-----------------
zhaoliu,34,402881c04223208801422320897a0024
-----------------
wangwu,45,402881c04223208801422320897a0023
-----------------
推荐使用HQL
|