2019年08月19日(星期一)  农历:己亥年七月十九

作者:三年。分类: IT杂谈

结论:

对于对象查询

1、使用List的时候会将对象全部取出,而使用Iterate则只先将对象主键取出,然后在使用的时候再一个个取出。

2、List第二使用的时候会继续重新数据库中取出,而Iterate则会先成缓存中查找,如果没找到再去数据库中取出。

对于属性查询(条件:查询缓存关闭):

两者没什么差别,根据查询对象的相应属性,在执行查询的时候将结果从数据库中取出(不同于上面据说的iterate在使用对象的时候取出),同时两者的第二次查询也是直接从数据库中取出数据,而不会从缓存中取得。

注意:一级缓存和二级缓存都只是存放实体对象的,如果查询实体对象的普通属性的数据,只能放到查询缓存里,查询缓存还存放查询实体对象的id.

hibernate的查询缓存默认是关闭的,如果要使用就要到hibernate.cfg.xml文件里配置:

<property name="hibernate.cache.use_query_cache">true</property>

并且必须在程序中手动启用查询缓存,在query接口中的setCacheable(true)方法来启用。

实践是检验真理的惟一标准:

Iterate实例(查询对象):

SessionFactory factory = new Configuration()。configure()。buildSessionFactory();

Session session=null;

session=factory.openSession();

session.beginTransaction();

Iterator iter = session.createQuery

("from User s where s.id<3")。iterate();

while (iter.hasNext()) {

User age = (User) iter.next();

System.out.println(age);

}

System.out.println("--------------------------------------");

iter = session.createQuery("from User s where s.id<3")。iterate();

while (iter.hasNext()) {

User age = (User) iter.next();

System.out.println(age);

}

session.getTransaction()。commit();

session.close();

结果如下:

Hibernate: select user0_.id as col_0_0_ from User user0_ where user0_.id<3

Hibernate: select user0_.id as id0_0_, user0_.username as username0_0_, user0_.age as age0_0_, user0_.password as password0_0_ from User user0_ where user0_.id=?

User [id=1, username=JAY, age=8, password=JAY]

Hibernate: select user0_.id as id0_0_, user0_.username as username0_0_, user0_.age as age0_0_, user0_.password as password0_0_ from User user0_ where user0_.id=?

User [id=2, username=aa, age=3, password=aa]

--------------------------------------

Hibernate: select user0_.id as col_0_0_ from User user0_ where user0_.id<3

User [id=1, username=JAY, age=8, password=JAY]

User [id=2, username=aa, age=3, password=aa]

从结果可知,第一次运行查询语句时,只查询了对象的id(主键),然后在每次使用具体对象时再进行查询取出(1+N);由于hibrenate缓存机制,第二次运行查询语句是只运行了id(主键)查询,对象结果却是从缓存中直接取得。

List实例(查询对象):

SessionFactory factory = new Configuration()。configure()。buildSessionFactory();

Session session=null;

session=factory.openSession();

session.beginTransaction();

List<User> list =(List<User>) session.createQuery

("from User s where s.id<3")。list();

for(User s:list){

System.out.println(s.toString());

}

System.out.println("--------------------------------------");

list = session.createQuery("from User s where s.id<3")。list();

for(User s:list){

System.out.println(s.toString());

}

session.getTransaction()。commit();

session.close();

结果如下:

Hibernate: select user0_.id as id0_, user0_.username as username0_, user0_.age as age0_, user0_.password as password0_ from User user0_ where user0_.id<3

User [id=1, username=JAY, age=8, password=JAY]

User [id=2, username=aa, age=3, password=aa]

--------------------------------------

Hibernate: select user0_.id as id0_, user0_.username as username0_, user0_.age as age0_, user0_.password as password0_ from User user0_ where user0_.id<3

User [id=1, username=JAY, age=8, password=JAY]

User [id=2, username=aa, age=3, password=aa]

从结果可知,两次执行查询语句时都是将所有对象一次取出,即使第二执行查询时也同样执行查询语句

混合情况实例(查询对象;第一次用list,第二次用iterate):

SessionFactory factory = new Configuration()。configure()。buildSessionFactory();

Session session=null;

session=factory.openSession();

session.beginTransaction();

List<User> list =(List<User>) session.createQuery

("from User s where s.id<3")。list();

for(User s:list){

System.out.println(s.toString());

}


System.out.println("--------------------------------------");

Iterator iter = session.createQuery("from User s where s.id<3")。iterate();

while (iter.hasNext()) {

User age = (User) iter.next();

System.out.println(age);

}

session.getTransaction()。commit();

session.close();

结果如下:

Hibernate: select user0_.id as id0_, user0_.username as username0_, user0_.age as age0_, user0_.password as password0_ from User user0_ where user0_.id<3

User [id=1, username=JAY, age=8, password=JAY]

User [id=2, username=aa, age=3, password=aa]

--------------------------------------

Hibernate: select user0_.id as col_0_0_ from User user0_ where user0_.id<3

User [id=1, username=JAY, age=8, password=JAY]

User [id=2, username=aa, age=3, password=aa]

从结果可知,第一次采用list查询,将对象全部取出,然后当第二次采用iterate查询时,只执行了对id(主键)的查询,然后通过主键从缓存中取出结果;可见采用list查询时在也会将结果放置缓存,只是它每次依旧从数据库中重新查询取得。

Iterate实例(查询对象属性):

SessionFactory factory = new Configuration()。configure()。buildSessionFactory();

Session session=null;

session=factory.openSession();

session.beginTransaction();

Iterator iter= session.createQuery

("select s.username from User s where s.id<3")。iterate();

while (iter.hasNext()) {

String age = (String) iter.next();

System.out.println(age);

}

System.out.println("--------------------------------------");

iter= session.createQuery

("select s.username from User s where s.id<3")。iterate();

while (iter.hasNext()) {

String age = (String) iter.next();

System.out.println(age);

}

session.getTransaction()。commit();

session.close();

结果如下:

Hibernate: select user0_.username as col_0_0_ from User user0_ where user0_.id<3

JAY

aa

--------------------------------------

Hibernate: select user0_.username as col_0_0_ from User user0_ where user0_.id<3

JAY

aa

从结果可知,当查询对象属性的时候,则是直接查询对象相应的属性,而不再查询对象id(主键);并且第二查询的时候是从数据库中重新查询取出结果,而不是从缓存中直接取出。

List实例(查询对象属性):

SessionFactory factory = new Configuration()。configure()。buildSessionFactory();

Session session=null;

session=factory.openSession();

session.beginTransaction();

List<String> list = session.createQuery

("select s.username from User s where s.id<3")。list();

for(String s:list){

System.out.println(s.toString());

}

System.out.println("--------------------------------------");

list =(List) session.createQuery

("select s.username from User s where s.id<3")。list();

for(String s:list){

System.out.println(s.toString());

}

session.getTransaction()。commit();

session.close();

结果如下:

Hibernate: select user0_.username as col_0_0_ from User user0_ where user0_.id<3

JAY

aa

--------------------------------------

Hibernate: select user0_.username as col_0_0_ from User user0_ where user0_.id<3

JAY

aa

结论和Iterate实例(查询对象属性)的结论一样

温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

已有 0/1595 人参与

发表评论:



手Q扫描加入Java初学者群