2013-12-12 64 views
-1

的計數訂購我有兩個實體,文件和評論:如何通過相關實體

@Entity 
@Table(name = "FILE") 
@SuppressWarnings("serial") 
@XmlRootElement 
public class File implements Serializable { 

    (...) 
    private Set<Commentary> commentaries = new HashSet<Commentary>(); 

    (...) 
    @OneToMany(mappedBy = "file") 
    public Set<Commentary> getCommentaries() { 
     return commentaries; 
    } 
} 

@Entity 
@Table(name = "COMMENTARY") 
@SuppressWarnings("serial") 
public class Commentary implements Serializable { 

(...) 
private File file; 

(...) 
@ManyToOne 
@JoinColumn(name = "FILE_ID") 
public File getFile() { 
    return file; 
} 

我想打一個HQL查詢來獲取文件,評註的每一個具有關聯的數量進行排序。我試過了:

SELECT f FROM FILE f WHERE f.name LIKE '%example%' ORDER BY COUNT(f.commentaries) 

沒有運氣。我如何正確使用HQL來做到這一點?

+1

您觀察到什麼錯誤? –

回答

0

我已經找到了答案。正確的HQL查詢是:

SELECT f FROM FILE f WHERE f.name LIKE '%example%' ORDER BY size(f.commentaries) 
0

試試這個

SELECT f FROM FILE f WHERE f.name LIKE '%example%' ORDER BY COUNT(commentaries)