HQL - 返回复杂对象列表

时间:2016-12-19 11:29:20

标签: java hql

目前有2个非常基本的类,其中一个嵌套在另一个类中。

班级工作:

public Class Job {
   private String name;
   private int salary;
   ... // Basic Constructors + Getters/Setters
}

班级公司:

public Class Company {
   private String name;
   private Collection<Job> jobs;
   ... // Basic Constructors + Getters/Setters
}

您可能已经猜到了,尝试进行一个查询,该查询将返回每个包含其作业集合的公司列表。

查询:

select COMPANY_NAME, JOB_NAME, count(*) FROM COMPANY_REPORT cr, JOB_DECLARATIONS jd
where cr.COMPANY_NAME = jd.COMPANY_NAME
group by cr.COMPANY_NAME , cr.JOB_NAME
order by cr.COMPANY_NAME , cr.JOB_NAME

会返回这样的内容:

Company Name | Job Name | Count
--------------------------------
Soni           Assistant    3
Soni           Manager      1
Zoom           Potographer  5

但我似乎无法理解它的HQL语法。

Select new com.stuff.Company(cr.COMPANY_NAME, ...how to add collection??) FROM ... 

1 个答案:

答案 0 :(得分:0)

Select  c.name, j.name, count(*) from company c, job j where c.name=:name    group by c.name, c.jobs
Order by c.name,c.jobs 

找到setparameterList here