Hibernate- 子查询
01.搭建开发环境
02.子查询
package com.gordon.test; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import com.gordon.domain.Publisher; import com.gordon.utils.HibernateUtil; /** * 子查询 * * @author Administrator */ public class TestDemo6 { /** * 子查询 * 查询结果: Hibernate: select publisher0_.id as id1_1_, publisher0_.name as name2_1_ from t_publisher publisher0_ where ( select count(*) from t_book books1_ where publisher0_.id=books1_.publisher_id )>1 Hibernate: select books0_.publisher_id as publishe4_0_0_, books0_.id as id1_0_0_, books0_.id as id1_0_1_, books0_.name as name2_0_1_, books0_.price as price3_0_1_, books0_.publisher_id as publishe4_0_1_ from t_book books0_ where books0_.publisher_id=? 电子工业出版社3 Hibernate: select books0_.publisher_id as publishe4_0_0_, books0_.id as id1_0_0_, books0_.id as id1_0_1_, books0_.name as name2_0_1_, books0_.price as price3_0_1_, books0_.publisher_id as publishe4_0_1_ from t_book books0_ where books0_.publisher_id=? 北京大学出版社2 Hibernate: select books0_.publisher_id as publishe4_0_0_, books0_.id as id1_0_0_, books0_.id as id1_0_1_, books0_.name as name2_0_1_, books0_.price as price3_0_1_, books0_.publisher_id as publishe4_0_1_ from t_book books0_ where books0_.publisher_id=? 人民邮电出版社2 */ @Test public void run1() { Session session = HibernateUtil.getCurrentSession(); Transaction transaction = session.beginTransaction(); String hql = "from Publisher p where (select count(*) from p.books) > 1"; List<Publisher> list = session.createQuery(hql).list(); for (Publisher publisher : list) { System.out.println(publisher.getName() + publisher.getBooks().size()); } transaction.commit(); } }