gpt4 book ai didi

java - Spring Boot - Jpa 独特且可分页

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:46 25 4
gpt4 key购买 nike

我有一个使用 kotlin 的 Spring Boot v 2.1.8.RELEASE 项目。我们使用 postgres 数据库,我有以下 JPA 查询

    @Query(value = " SELECT distinct grievance " +
" FROM Grievance grievance" +
" WHERE (grievance.auditProgramPortal=:auditProgramPortal AND grievance.owner.id=:organizationId) " +
" OR (grievance.status=:status AND grievance.auditProgramPortal=:auditProgramPortal)" +
" OR (grievance.auditProgramPortal=:auditProgramPortal AND grievance.owner.id=:organizationId)" +
" OR (grievance.auditProgramPortal=:auditProgramPortal AND grievance.publicSubmission=:publicSubmission)")
fun findAnalystBodyGrievances(
auditProgramPortal: Boolean,
organizationId: Long,
status: GrievanceStatus,
publicSubmission: Boolean,
pageable: Pageable): Page<Grievance>

当我按原样运行时 - 它工作正常。当我对行业字段进行排序时(请参阅下面的对象)- 我收到以下错误

org.postgresql.util.PSQLException: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

相关对象摘录如下

申诉

@Entity
@Audited
@Table(name = "grievance")
class Grievance {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
var id: Long? = null

@Column(name = "title", nullable = false)
var title: String? = null

@Column(name = "description", columnDefinition = "text")
var description: String? = null

@Column(name = "public_description", columnDefinition = "text")
var publicDescription: String? = null

@Column(name = "related_to_identified_risk")
var relatedToIdentifiedRisk: Boolean? = null


@OneToOne
@JoinColumn(name = "industry_id")
var industry: Industry? = null
}

行业

@Entity
@Table(name = "industry")
class Industry {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
var id: Long? = null

@Column(name = "name", nullable = false, unique = true)
var name: String? = null

@Column(name = "color", nullable = false, unique = false)
var color: String? = null
}

我的问题是 - 修复查询以便仍返回页面的最简单方法是什么如果我更新我的代码以使用以下查询 - 返回一个通用对象

@Query(value = " SELECT distinct grievance, grievance.industry " +
" FROM Grievance grievance" +
" WHERE (grievance.auditProgramPortal=:auditProgramPortal AND grievance.owner.id=:organizationId) " +
" OR (grievance.status=:status AND grievance.auditProgramPortal=:auditProgramPortal)" +
" OR (grievance.auditProgramPortal=:auditProgramPortal AND grievance.owner.id=:organizationId)" +
" OR (grievance.auditProgramPortal=:auditProgramPortal AND grievance.publicSubmission=:publicSubmission)")

非常感谢对此的任何帮助

最佳答案

试试下面

@Query(value =" Select grievance from (SELECT distinct grievance, grievance.industry " +
" FROM Grievance grievance" +
" WHERE (grievance.auditProgramPortal=:auditProgramPortal AND grievance.owner.id=:organizationId) " +
" OR (grievance.status=:status AND grievance.auditProgramPortal=:auditProgramPortal)" +
" OR (grievance.auditProgramPortal=:auditProgramPortal AND grievance.owner.id=:organizationId)" +
" OR (grievance.auditProgramPortal=:auditProgramPortal AND grievance.publicSubmission=:publicSubmission))")

关于java - Spring Boot - Jpa 独特且可分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57996331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com