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();
	}
}

 

转载于:https://www.cnblogs.com/hfultrastrong/p/7421511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值