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();
    	}
    }
    
  • 相关阅读:
    html抽取文本信息-java版(适合lucene建立索引)
    【LeetCode with Python】 Sort List
    POJ 2533 Longest Ordered Subsequence(dp LIS)
    Activity 之间 传递 List 封装的对象或者对象
    mongo数据库--非关系型数据库
    cocos2d-x的声音控制
    CSDN博客积分规则
    怎样使用递归实现归并排序
    android中9-patch图片的使用
    Cocos2d-x-3.0环境搭建
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/7421511.html
Copyright © 2011-2022 走看看