博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hibernate 级联查询
阅读量:6911 次
发布时间:2019-06-27

本文共 2601 字,大约阅读时间需要 8 分钟。

级联查询

1,实体类结构

Java代码  
  1. @Entity  
  2. @Table(name = "t_vote")  
  3. public class Vote {  
  4.     private int id;  
  5.     /*** 
  6.      * 1:最宜居<br> 
  7.      * 2:最优户<br> 
  8.      * 3:最佳物业 
  9.      */  
  10.     private int type;  
  11.     private HouseBuilding houseBuilding;  
  12.     /*** 
  13.      * 投票数 
  14.      */  
  15.     private long voteCount;  
  16. @OneToOne  
  17.     @JoinColumn(name = "house_building_id")  
  18.     public HouseBuilding getHouseBuilding() {  
  19.         return houseBuilding;  
  20.     }  
  21. }  

 

Java代码  
  1. @Entity  
  2. @Table(name = "t_house")  
  3. public class HouseBuilding {  
  4.     private int id;  
  5.     private String name;  
  6.     private String address;  
  7.     private Float price;  
  8.     /*** 
  9.      * 预留 
  10.      */  
  11.     private String reserved;  
  12. }  

 

 

 

2,关系

Vote和HouseBuilding 是一对一的外键关系,从Vote 可以导航到HouseBuilding,反之不能.

 

3,查询语句(实例)

Java代码  
  1. Vote vote=super.get("type", type,"houseBuilding.id",houseBuildingId);  

 super.get 方法体如下:

Java代码  
  1. public T get(String propertyName,Object propertyValue,String propertyName2,Object propertyValue2){  
  2.         return (T)this.getCurrentSession().createCriteria(clz)  
  3.                 .add(Restrictions.eq(propertyName, propertyValue))  
  4.                 .add(Restrictions.eq(propertyName2, propertyValue2))  
  5.                 .uniqueResult();  
  6.     }  

 所以实际上相当于:

Java代码  
  1. Vote vote=(Vote) super.getCurrentSession().createCriteria(clz)  
  2.         .add(Restrictions.eq("type", type))  
  3.         .add(Restrictions.eq("houseBuilding.id",houseBuildingId))  
  4.         .uniqueResult();  

 

类似于:

Java代码  
  1. Vote vote=(Vote) super.getCurrentSession().createCriteria(clz)  
  2.         .add(Restrictions.eq("type", type))  
  3.         .createAlias("houseBuilding""houseBuilding222")  
  4.         .add(Restrictions.eq("houseBuilding222.id", houseBuildingId))  
  5.         .uniqueResult();  

 

 

4,执行的SQL 语句

Sql代码  
  1. select  
  2.         this_.id as id1_21_1_,  
  3.         this_.house_building_id as house4_21_1_,  
  4.         this_.type as type2_21_1_,  
  5.         this_.vote_count as vote3_21_1_,  
  6.         housebuild2_.id as id1_9_0_,  
  7.         housebuild2_.address as address2_9_0_,  
  8.         housebuild2_.name as name3_9_0_,  
  9.         housebuild2_.price as price4_9_0_,  
  10.         housebuild2_.reserved as reserved5_9_0_   
  11.     from  
  12.         t_vote this_   
  13.     left outer join  
  14.         t_house housebuild2_   
  15.             on this_.house_building_id=housebuild2_.id   
  16.     where  
  17.         this_.type=?   
  18.         and this_.house_building_id=?  
  19. 07 十月 2015 10:04:22,589 TRACE org.hibernate.type.descriptor.sql.BasicBinder:84 - binding parameter [1] as [INTEGER] - 1  
  20. 07 十月 2015 10:04:22,590 TRACE org.hibernate.type.descriptor.sql.BasicBinder:84 - binding parameter [2] as [INTEGER] - 3  

 

 

5,使用Restrictions.eq 来进行条件查询时,第一个参数可以采用"属性.子属性"的形式

6,在单元测试中,加载hibernate配置文件

Java代码  
  1. @BeforeClass  
  2.     public static void before() {  
  3.         ctx = new ClassPathXmlApplicationContext("beans.xml");  
  4. //      clientVersionDao = (ClientVersionDao) ctx.getBean("clientVersionDao");  
  5. //      oSVersionDao = (OSVersionDao) ctx.getBean("osVersionDao");  
  6. //      osTypeDao = (OsTypeDao) ctx.getBean("osTypeDao");  
  7.         paperNewsDao = (PaperNewsDao) ctx.getBean("paperNewsDao");  
  8.         voteDao = (VoteDao) ctx.getBean("voteDao");  
  9.     }  

 

转载地址:http://qabcl.baihongyu.com/

你可能感兴趣的文章
怎样区别nginx中rewrite时break和last
查看>>
开发HBase的时候需要搭建的Eclipse总结
查看>>
Mysql数据库修复
查看>>
我的友情链接
查看>>
leetCode 70. Climbing Stairs | 动态规划
查看>>
bboss标签使用大全-数据展示标签
查看>>
java中hashCode()与equals()详解
查看>>
点滴积累【JS】---JS小功能(createElement和insertBefore添加div下面的节点)
查看>>
异步提交form表单
查看>>
A Newbie’s Install of Keras & Tensorflow on Windows 10 with R
查看>>
关于使用input type=file 标签上传文件的注意细节(上传文件 无法获取文件 问题)...
查看>>
<if test="outState!=null">OUT_STATE=#{outState},</if>空格问题
查看>>
.Net内存回收
查看>>
js 获取/设置文本输入域内光标的位置的方法
查看>>
oracle sql developer 出现 : 适配器无法建立连接问题解决方案 The Network Adapter could not establish the connection...
查看>>
Linux下connect超时处理【总结】
查看>>
高性能数据库集群:读写分离
查看>>
Laravel 5.5 Blade::if 简介
查看>>
centos7搭建ELK Cluster集群日志分析平台(三):Kibana
查看>>
UITextField 监听内容变更解决方案
查看>>