Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取错误,org.postgresql.util.psqleexception:error:syntax error位于或接近"&引用,在hibernate中尝试将列表传递给新类时_Java_Hibernate_Spring Boot_Kotlin_Spring Data Jpa - Fatal编程技术网

Java 获取错误,org.postgresql.util.psqleexception:error:syntax error位于或接近"&引用,在hibernate中尝试将列表传递给新类时

Java 获取错误,org.postgresql.util.psqleexception:error:syntax error位于或接近"&引用,在hibernate中尝试将列表传递给新类时,java,hibernate,spring-boot,kotlin,spring-data-jpa,Java,Hibernate,Spring Boot,Kotlin,Spring Data Jpa,我想在HQL查询中构造一个包含类的列表,但每当我试图将列表传递给新类时,SQL生成都会失败,因为它会告诉我org.postgresql.util.PSQLException:ERROR:syntax ERROR at或near.“。最终结果应该是一个表单对象列表,其中每个表单都包含一个投票列表 如果我用选择new com.my.class.Projection(q.id,q.otherId,q.voces)启动查询,q.voces将在SQL中生成为,从而导致错误。但是,如果我将投影的投票列表更改

我想在HQL查询中构造一个包含类的列表,但每当我试图将列表传递给新类时,SQL生成都会失败,因为它会告诉我
org.postgresql.util.PSQLException:ERROR:syntax ERROR at或near.“
。最终结果应该是一个表单对象列表,其中每个表单都包含一个投票列表

如果我用
选择new com.my.class.Projection(q.id,q.otherId,q.voces)
启动查询,q.voces将在SQL中生成为
,从而导致错误。但是,如果我将投影的投票列表更改为int(并将查询更改为
SELECT new com.my.class.Projection(q.id,q.otherId,q.voces.size)
只是为了获得投票的大小,它会告诉我投票的大小。为什么我可以检索投票的数量,而不能检索列表本身?任何帮助都将不胜感激

将生成的SQL的一部分:

Hibernate: select question0_.id as col_0_0_, question0_.other_id as col_1_0_, . as col_2_0_ from public.questions`

源代码:

问题库

interface QuestionRepository : JpaRepository<Question, Long> {
    @Query("SELECT new com.my.class.Projection(q.id, q.otherId, q.votes) FROM Question q WHERE q.form = :form")
    fun findQuestionSimple(@Param("form") form: Form): List<Projection>

DTO投影的主要目的是有效地将元组映射到DTO类中。我认为它不适用于关联

现在,为了解决您的特定问题,我将做以下几点:

  • 将查询更改为仅选择问题对象
  • 然后更改
    投影
    构造函数

  • 虽然我不认为这是最好的方法,但这是可行的。似乎你想做一些转换,这是a的目的。

    DTO投影的主要目的是有效地将元组映射到DTO类中。我认为它不适用于关联

    现在,为了解决您的特定问题,我将做以下几点:

  • 将查询更改为仅选择问题对象
  • 然后更改
    投影
    构造函数
  • 虽然我不认为这是最好的方法,但这会起作用。似乎你想做一些转换,这是一个项目的目的

    data class Projection(
        var id: Long = 0,
        var otherId: String
    ) {
        @OneToMany(mappedBy = "question")
        var votes: List<VoteProjection>? = listOf()
    
        constructor(id: Long, otherId: String, votes: List<Vote>) : this(id, otherId) {
            this.id = id
            this.otherId = otherId
            this.votes = votes.map { VoteProjection(it.id, if (it.user !== null) VoteGuestProjection(it.user!!.id) else null) }
        }
    }
    
    data class VoteProjection(
        var id: Long = 0,
        var user: VoteUserProjection?
    )
    
    data class VoteUserProjection(
        var id: Long = 0
    )
    
    @Entity
    @Table(name = "questions", schema = "PUBLIC")
    class Question {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id: Long = 0
    
        @NotNull
        var otherId = ""
    
        @ManyToOne
        @JsonIgnore
        @JoinColumn
        var form: Form? = null
    
        @OneToMany(mappedBy = "form")
        @JsonIgnoreProperties("form")
        var votes: List<Vote> = listOf()
    }
    
    @Entity
    @Table(name = "votes", schema = "PUBLIC")
    class Vote {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id: Long = 0
    
        @ManyToOne
        @JoinColumn
        var user: User? = null // Not relevant for this question
    }
    
    @Query("SELECT new com.my.class.Projection(q) FROM Question q WHERE q.form = :form")
    
    constructor(q: Question) : this(id, otherId) {
        this.id = q.id
        this.otherId = q.otherId
        this.votes = q.votes.map { VoteProjection(it.id, if (it.user !== null) VoteGuestProjection(it.user!!.id) else null) }
    }