HQL (二) 实体对象查询
实体对象查询(重要)
* N+1问题,在默认情况下使用query.iterate查询,有可能出现N+1问题,所谓的N+1
是查询对象的时候发出了N+1条sql语句
1:首先发出一条查询id列表的sql
N:根据id列表发出n条查询语句
* list和iterate的区别?
* list在默认情况下,只向缓存中放入数据,而不利用缓存中的数据
* iterate在默认情况下有N+1问题,如果缓存中存在数据那么会根据id到缓存获取数据
也就说iterate是利用缓存的
参见:SimpleObjectQueryTest1.java,SimpleObjectQueryTest2.java
- package com.wlh.hibernate;
- import java.util.Iterator;
- import java.util.List;
- import junit.framework.TestCase;
- import org.hibernate.Query;
- import org.hibernate.Session;
- public class SimpleObjectQueryTest1 extends TestCase{
- public void testQuery1(){
- Session session=null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- //返回值为实体对象的集合
- //可以省略select语句
- Query query=session.createQuery("from Student");
- List students=query.list();
- for(Iterator iter=students.iterator();iter.hasNext();){
- Student student=(Student)iter.next();
- System.out.println("student="+student.getName());
- }
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- public void testQuery2(){
- Session session=null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- //返回值为实体对象的集合
- //可以省略select语句,实体类可以加别名
- Query query=session.createQuery("from Student s");
- List students=query.list();
- for(Iterator iter=students.iterator();iter.hasNext();){
- Student student=(Student)iter.next();
- System.out.println("student="+student.getName());
- }
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- public void testQuery3(){
- Session session=null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- //返回值为实体对象的集合
- //可以省略select语句,实体类可以用as加别名
- Query query=session.createQuery("from Student as s");
- List students=query.list();
- for(Iterator iter=students.iterator();iter.hasNext();){
- Student student=(Student)iter.next();
- System.out.println("student="+student.getName());
- }
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- public void testQuery4(){
- Session session=null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- //返回值为实体对象的集合
- //使用select查询实体对象,必须采用别名
- Query query=session.createQuery("select s from Student as s");
- List students=query.list();
- for(Iterator iter=students.iterator();iter.hasNext();){
- Student student=(Student)iter.next();
- System.out.println("student="+student.getName());
- }
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }
- package com.wlh.hibernate;
- import java.util.Iterator;
- import java.util.List;
- import junit.framework.TestCase;
- import org.hibernate.Query;
- import org.hibernate.Session;
- public class SimpleObjectQueryTest2 extends TestCase{
- /**
- * 发出list查询
- */
- public void testQuery1(){
- Session session=null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- /**
- * 采用list查询将会发出一条查询语句,取得Student数据
- * select student0_.id as id1_, student0_.name as name1_,
- * student0_.createTime as createTime1_, student0_.classesid as classesid1_
- * from t_student student0_
- *
- */
- Query query=session.createQuery("from Student");
- List students=query.list();
- for(Iterator iter=students.iterator();iter.hasNext();){
- Student student=(Student)iter.next();
- System.out.println("student="+student.getName());
- }
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- /**
- * 出现N+1问题
- *
- * 发出查询id类表的sql语句:
- * select student0_.id as col_0_0_ from t_student student0_
- *
- * 依次发出根据id查询Student的sql语句:
- * select student0_.id as id1_0_, student0_.name as name1_0_,
- * student0_.createTime as createTime1_0_, student0_.classesid as classesid1_0_
- * from t_student student0_ where student0_.id=?
- *
- */
- public void testQuery2(){
- Session session=null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- Query query=session.createQuery("from Student");
- Iterator iter=query.iterate();
- while(iter.hasNext()){
- Student student=(Student)iter.next();
- System.out.println("student="+student.getName());
- }
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- /**
- * 先发出list查询,再发出iterate查询
- */
- public void testQuery3(){
- Session session=null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- Query query=null;
- query=session.createQuery("from Student");
- List students=query.list();
- for(Iterator iter=students.iterator();iter.hasNext();){
- Student student=(Student)iter.next();
- System.out.println("student="+student.getName());
- }
- System.out.println("===============================");
- /**
- * 不会出新N+1问题
- *
- * 因为list操作已经将对象放到了一级缓存中(session级缓存),所以在使用Iterate操作的时候
- * 它会发出根据id查询的语句,取得id列表,再根据id到缓存中获取数据,只有在缓存中找不到相应的数据
- * 才会发出sql语句到数据库中查询
- */
- query=session.createQuery("from Student");
- Iterator iter=query.iterate();
- while(iter.hasNext()){
- Student student=(Student)iter.next();
- System.out.println("student="+student.getName());
- }
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- /**
- * 发出两次list查询
- */
- public void testQuery4() {
- Session session = null;
- try {
- session = HibernateUtils.getSession();
- session.beginTransaction();
- List students = session.createQuery("from Student").list();
- for (Iterator iter =students.iterator(); iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- System.out.println("------------------------------------------------");
- /**
- * 再次发出查询语句
- *
- * 在默认情况下list每次都会向数据库发出查询对象的sql,除非配置了查询缓存,所以下面的list
- * 操作,虽然在一级缓存中已经有了对象数据,list不会利用缓存中的数据,而再次发出查询sql
- *
- * list在默认情况下,只向缓存中放入数据,而不利用缓存中的数据
- */
- students = session.createQuery("from Student").list();
- for (Iterator iter =students.iterator(); iter.hasNext();) {
- Student student = (Student)iter.next();
- System.out.println(student.getName());
- }
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- session.getTransaction().rollback();
- }finally {
- HibernateUtils.closeSession(session);
- }
- }
- }