2015-12-24 39 views
1

我有以下實體:HQL用於獲取分鐘

Company.class:

public class Company { 
    @JoinTable(name = "company_employee", joinColumns = @JoinColumn(name = "company_id") , inverseJoinColumns = @JoinColumn(name = "employee_id")) 
    @ManyToMany(fetch = FetchType.LAZY) 
    private Set<Employee> employees; 

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "company") 
private List<Score> scores; 

    @JoinTable(name = "company_factor", joinColumns = @JoinColumn(name = "company_id") , inverseJoinColumns = @JoinColumn(name = "factor_id")) 
    @ManyToOne(fetch = FetchType.LAZY) 
    private Factor factors; 
} 

和Employee.class

public class Employee { 
      @ManyToMany(fetch = FetchType.EAGER, mappedBy="employees") 
      private Set<Company> companies; 

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "company") 
    private List<Score> scores; 

      @JoinTable(name = "employee_factor", joinColumns = @JoinColumn(name = "employee_id") , inverseJoinColumns = @JoinColumn(name = "factor_id")) 
      @ManyToMany(fetch = FetchType.LAZY) 
      private Set<Factor> factors; 
@Transient 
    private int score; 
    } 

Factor.class不包含任何關係。

此外,我有一些實體評分是每個組合公司員工獨特的實體評分。 它看起來像這樣: Score.class:

@ManyToOne(fetch=FetchType.LAZY) 
    @JoinColumn(name = "company_id", insertable = false, updatable = false) 
    private Company company; 

    @ManyToOne(fetch=FetchType.EAGER) 
    @JoinColumn(name = "employee_id", insertable = false, updatable = false) 
    private Employee employee; 

    @Column(name = "score") 
    private BigDecimal score; 

如果我得到的名單,這將是組合公司和員工實例的列表,有時公司或員工可以重複。 目標是獲得列表,按員工中的因子進行過濾,並僅顯示按升序排列的每位員工的最低分數。 說,如果存在組合

employee1-company1, score=1 
employee1-company2, score=2 
employee2-company1, score=3 
employee2-company4, score=5 
employee3-company4, score6 

ResultList應該看起來像:

employee1-company1, score=1 
employee2-company1, score=3 
employee3-company4, score=6 

所以員工應該不會重複,但公司可以在列表中重複。 我不太確定,該怎麼做。我所取得的成績是以升序顯示獨特的結果,但他們沒有顯示最低分數。我使用HQL:

select distinct e from Score e 
left outer join fetch e.company 
left outer join fetch e.company.factors 
left outer join fetch e.employee 
left outer join fetch e.employee.factors ef 
where ef.factor_id=:factor_id 
group by e.employee.employee_id 
order by e.score asc 

任何人都可以幫助如何實現我所需要的?謝謝。

UPDATE1:

我決定走另一條路。

select distinct e from Employee e join e.scores es order by es.score asc 

現在看來,這正是我需要的,但如何在查詢中把最低es.score到現場比分Employee對象: 現在我通過員工使用此查詢得到它?也許有一種方法可以用es.score替代e.score?通過對上述獲取ASC的結果(默認)得分次序

  • 組:

  • 回答

    0

    作爲解決方案,我切換到entityManager.createNativeQuery("some native sql string")。 我對結果很滿意。只是爲了這種情況下,有關SQL查詢的問題是here 唯一的缺點是不可能使用join fetch,因此N + 1選擇問題在這裏,但我打算獲取相當小的數據塊,因此它是可承受的。

    0

    您可以使用以下方法:

    Select a from (Select b from Score b order by score) as a group by a.employee 
    

    說明:

    • Select b from Score b order by score結果會給予獨特的員工

    +0

    不在JPA中,因爲您不能將子查詢作爲FROM。 –

    +0

    謝謝,但它不起作用,說'('是意外的標記:'org.springframework.dao.InvalidDataAccessApiUsageException:org.hibernate.hql.internal.ast.QuerySyntaxException:意外的標記:'只爲案件:我在'entityManager.createQuery()'中使用它 –