Java 如何按相关实体的计数进行订购
我有两个实体,文件和评论:Java 如何按相关实体的计数进行订购,java,hibernate,sql-order-by,hql,Java,Hibernate,Sql Order By,Hql,我有两个实体,文件和评论: @Entity @Table(name = "FILE") @SuppressWarnings("serial") @XmlRootElement public class File implements Serializable { (...) private Set<Commentary> commentaries = new HashSet<Commentary>(); (...) @OneToMany
@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(commentaries)
我找到了答案。正确的HQL查询是:
SELECT f FROM FILE f WHERE f.name LIKE '%example%' ORDER BY size(f.commentaries)
你观察到了什么错误?
SELECT f FROM FILE f WHERE f.name LIKE '%example%' ORDER BY size(f.commentaries)