当前位置:编程学习 > JAVA >>

Hibernate三表联合查询问题,在线求解答

需求是这样的:
三张表,第一张基础表公司基本信息t_FcompanyBaseinfo(id,name,..),两张关联表t_fcompanyBusinessincomes (id,fcoid,income,year..),
t_fcompanyStructStrategys (id,fcoid,employeenumber,year..),
两张关联表和主表都是N-1,fcoid外键关联Base的id,
现在要得到某个year的公司income(ExtendA中),以及employeenumber(ExtendB中),
我做了两个测试:
1

select new oa.Fcompanyextrainfo(f.id,f.name,fs.employeeNumber,fb.Income) from FcompanyBaseinfo  f inner join f.fcompanyBusinessincomes fb inner join f.fcompanyStructStrategys fs where fs.year='2006' and fb.year='2006';

2

select new oa.Fcompanyextrainfo(f.id,f.name,fs.employeeNumber,fb.Income) from FcompanyBaseinfo  f left join f.fcompanyBusinessincomes fb left join f.fcompanyStructStrategys fs where fs.year='2006' and fb.year='2006';

Hibernate后台生成的SQL为:

 select
        fcompanyba0_.ID as col_0_0_,
        fcompanyba0_.NAME as col_1_0_,
        fcompanyst2_.EMPLOYEE_NUMBER as col_2_0_,
        fcompanybu1_.BUSINESS_INCOME as col_3_0_,
        fcompanybu1_.MARKET_SHARE as col_4_0_,
        fcompanybu1_.OPERATING_PROFIT as col_5_0_ 
    from
        T_FCOMPANY_BASEINFO fcompanyba0_,
        T_FCOMPANY_BUSINESSINCOME fcompanybu1_,
        T_FCOMPANY_STRUCT_STRATEGY fcompanyst2_ 
    where
        fcompanyba0_.ID=fcompanybu1_.FCO_ID 
        and fcompanyba0_.ID=fcompanyst2_.FCO_ID 
        and fcompanyst2_.YEAR='2010' 
        and fcompanybu1_.YEAR='2010'

始终得不到结果,求指教
原生的sql语句应该是这样的:

select
        fcompanyba0_.ID as col_0_0_,
        fcompanyba0_.NAME as col_1_0_,
        fcompanyst2_.EMPLOYEE_NUMBER as col_2_0_,
        fcompanybu1_.BUSINESS_INCOME as col_3_0_,
        fcompanybu1_.MARKET_SHARE as col_4_0_,
        fcompanybu1_.OPERATING_PROFIT as col_5_0_ 
    from
        T_FCOMPANY_BASEINFO fcompanyba0_
      left join
        T_FCOMPANY_BUSINESSINCOME fcompanybu1_ on                fcompanyba0_.ID=fcompanybu1_.FCO_ID 
and fcompanybu1_.YEAR='2010'
        left join
        T_FCOMPANY_STRUCT_STRATEGY fcompanyst2_ on  fcompanyba0_.ID=fcompanyst2_.FCO_ID 
and fcompanyst2_.YEAR='2010'

怎么写HQL,在线急求解答。。。。 --------------------编程问答-------------------- 表名 notify_document
描述 收(退)货通知单
字段 类型 长度 主键/索引 是否为空 描述
id int 主键 否 记录编号
code string 18 索引 是 单号(品牌编码[8]+年[4]+月[2]+日[2]+序号[2])
type int 索引 是 类型(0-退货 1-收货)
suppliers_id int 索引 是 供货商Id
suppliers_code string 32 索引 是 供货商编码(8)
suppliers string 64 是 供货商
plan_date string 16 是 计划收(退)货日期
submit_id int 索引 是 通知单提交人Id
submit_name string 32 索引 是 通知单提交人帐号
submit_man string 64 是 通知单提交人
submit_time datetime 是 提交时间
begin_time datetime 是 入(出)库开始时间
end_time datetime 是 入(出)库完成时间
status int 索引 是 状态(0-新建 1-正在入库[出库] 2-完成)
remind_remark string 1024 是 提醒备注
is_valid int 索引 是 是否有效记录(0-无效 1-有效)
invalid_date datetime 是 删除时间



表名 product
描述 商品表
字段 类型 长度 主键/索引 是否为空 描述
id int 主键 否 记录编号
code string 32 索引 是 商品编码
title string 64 是 商品名称
barcode string 128 索引 是 商品条码
sort string 128 索引 是 分类
suppliers_id int 索引 是 商品供货商Id
suppliers_code string 32 索引 是 商品供货商编码
suppliers string 64 是 商品供货商
sale_unit string 32 是 商品单位
price string 32 是 单价
shelf_life int 索引 是 保质期
min_store int 索引 是 警戒库存
storehouse_id int 索引 是 仓库Id
storehouse_code string 32 索引 是 仓库编码
storehouse string 64 是 仓库
is_batch int 索引 是 是否批次管理(0-否 1-是)
is_valid int 索引 是 是否有效记录(0-无效 1-有效)
invalid_date datetime 是 删除时间


表名 notify_document_detail
描述 收(退)货通知单(明细)
字段 类型 长度 主键/索引 是否为空 描述
id int 主键 否 记录编号
document_id int 索引 是 收(退)货通知单Id
document_code string 18 索引 是 收(退)货通知单编号
product_code string 32 索引 是 商品编码(8)
product_title string 256 是 商品名称
product_bar_code string 32 索引 是 商品条码
product_sort string 32 索引 是 商品分类
product_unit string 16 是 商品销售单位
product_price string 32 是 商品销售单价
storehouse_id int 索引 是 仓库Id
storehouse_code string 8 索引 是 仓库编码
storehouse_title string 32 是 仓库名称
num int 是 收(退)货数量
status int 索引 是 状态(0-未入(出)库 1-已入(出)库)
is_valid int 索引 是 是否有效记录(0-无效 1-有效)
invalid_date datetime 是 删除时间



怎么查出
  供货商编码、供货商、计划收(退)货日期、商品编码、仓库、商品名称、收(退)货数量

这些信息
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,