• 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();
    	}
    }
    
  • 相关阅读:
    [转]ThinkCMF框架任意内容包含漏洞分析复现
    nodejs环境安装
    ffmpeg+nginx-rtmp-module
    activemq安装
    mysql审计插件
    sql优化工具SQLAdvisor的安装
    Harbor安装(docker-compose) -- 企业级Registry仓库
    在CentOS上部署kubernetes1.9.0集群
    3-基础概念(数据模型、时序 4 种类型、作业和实例)
    1-什么是 Prometheus
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/7421511.html
Copyright © 2020-2023  润新知