如何返回JPA表中不存在的计数列
我想找到一种方法来获取额外的列来计算我的记录,并将其返回到一个带有额外字段的映射实体中。 我在字段上尝试了@transient,但在查询时它不会返回值。 然后我删除@transient,但在保存时得到一个异常。 我还尝试了@Formula,但收到空指针异常。 以下是我的存储库代码:如何返回JPA表中不存在的计数列,jpa,Jpa,我想找到一种方法来获取额外的列来计算我的记录,并将其返回到一个带有额外字段的映射实体中。 我在字段上尝试了@transient,但在查询时它不会返回值。 然后我删除@transient,但在保存时得到一个异常。 我还尝试了@Formula,但收到空指针异常。 以下是我的存储库代码: @Query(value = "select id,account,session_id,create_time,count from query_history a join " +
@Query(value = "select id,account,session_id,create_time,count from query_history a join " +
"(select session_id sessionId,max(create_time) createTime,count(*) count from query_history group by session_id) b " +
"on a.session_id = b.sessionId and a.create_time = b.createTime where account = ?1 order by create_time desc",
countQuery = "select count(distinct(session_id)) from query_history where account = ?1",
nativeQuery = true)
Page<QueryHistory> findByNtAndGroupBySessionAndAction(String account, Pageable pageable);
对不起,我的英语不好,非常感谢您的建议。我通过投影解决了这个问题,事实上我以前也尝试过,但在我的sql中:
select id,account,session_id,create_time,count
应该是:
select id,account,session_id sessionId,create_time createTime,count
附言:
投影界面:
public interface QueryHistoryWithCountProjection {
Long getId();
String getAccount();
Long getSessionId();
long getCreateTime();
Integer getCount();
}
public interface QueryHistoryWithCountProjection {
Long getId();
String getAccount();
Long getSessionId();
long getCreateTime();
Integer getCount();
}