【问题标题】:How mapping List<Long> with array_agg?如何映射 List<Long> 与 array_agg?
【发布时间】:2020-07-14 17:17:51
【问题描述】:

我使用 Hibernate 执行此请求 @Subselect

SELECT сolumn1, array_agg(DISTINCT сolumn2::integer) FROM 表 GROUP BY сolumn1;

结果 SQL:

 сolumn1 | сolumn2 
------------+---------------------
          1 | {1}                 
          2 | {2,3}               
          3 | {3}  

           

我的 DAO 类:

enter image description here

import lombok.Data;
import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Subselect;


import javax.persistence.*;

@Data
@Entity
@Subselect("SELECT сolumn1,\n" +
        "\tarray_agg(DISTINCT сolumn2::integer)\n" +
        "FROM table GROUP BY сolumn1;")
@Immutable
public class Table {

    @Id
    @Column(name = "сolumn1")
    private Long сolumn1;

    @Column(name = "сolumn2")
    private Integer[] сolumn2;
}

现在不行了,出现错误:

message": "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize

List如何与array_agg映射?

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    您的 Interger[] column2 的反序列化存在问题。将其替换为Collection&lt;Integer&gt;

    这应该可以让它成功映射。

    Mapping array with Hibernate

    【讨论】:

    • 从 java.util.Collection 添加集合?
    • 正确,您必须导入 java.util.Collection。链接的问题有帮助吗?
    • 很遗憾,没有,类似的错误。需要使用 Array 或 PgArray from import java.sql.Array;
    • 你使用collection的时候出现什么错误?现在,您必须在使用时将集合转换为数组。
    猜你喜欢
    • 2013-05-08
    • 2019-11-27
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    相关资源
    最近更新 更多