0
当我使用hibernate的条件执行查询时遇到问题。生成的SQL很简单:错误:列“this_.created_time”必须出现在GROUP BY子句中或用于聚合函数
/* criteria query */
select
count(*) as y0_
from
cx_vss_video this_
where
this_.uploader_id=?
order by
this_.created_time asc
它抱怨错误:
Caused by: org.postgresql.util.PSQLException: ERROR: column "this_.created_time" must appear in the GROUP BY clause or be used in an aggregate function
Position: 105
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
有一个存在samilar问题here。但其答案似乎非常复杂,并且不合理。那么还有其他解决方案
我的实体类:
@Entity
@Table(name = "cx_vss_video")
public class Video {
public enum TranscodingStatus {
NOT_START, TRANSCODING, SUCCESS, ERROR, CANCELED;
}
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cx_vss_video_seq")
@SequenceGenerator(name = "cx_vss_video_seq", sequenceName = "cx_vss_video_seq")
private Long id;
@Column(name = "uploader_name", nullable = false)
private String uploaderName;
@Column(name = "file_name")
@NaturalId
private String fileName;
@Column(name = "created_time")
private Date createdTime = new Date();
@Column(name = "transcoding_status")
@Enumerated(EnumType.STRING)
private TranscodingStatus transcodingStatus = TranscodingStatus.TRANSCODING;
}
我可以file_name
normaly秩序,而且当created_time
下令没有运气。我很困惑问题出现在哪里?当我使用H2时,问题也会出现。
编辑 我终于通过删除命令参数解决了问题,它现在可以工作。
CriteriaImpl criteriaImpl = criteria instanceof CriteriaImpl ? (CriteriaImpl) criteria : null;
if (criteriaImpl != null) {
Iterator<OrderEntry> it = criteriaImpl.iterateOrderings();
while (it.hasNext()) {
it.next();
it.remove();
}
criteria.setProjection(Projections.rowCount());
totalResults = (long) criteria.uniqueResult();
}