zoukankan      html  css  js  c++  java
  • 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();
    	}
    }
    
  • 相关阅读:
    License for package Android SDK Build-Tools 28.0.3 not accepted
    React实现座位排布组件
    Flutter中Expanded组件用法
    Ant Design Pro路由传值
    Ant Design中getFieldDecorator方法的特殊用法(小bug)
    React子组件和父组件通信
    Ant Design Pro项目打开页设为登录或者其他页面
    JS中的splice方法
    Ant Design 表单中getFieldDecorator、getFieldValue、setFieldValue用法
    c++下标越界问题探讨
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/7421511.html
Copyright © 2011-2022 走看看