3

I execute this request @Subselect using Hibernate

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

result SQL:

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

           

My DAO class:

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;
}

Now it does not work, an error occurs:

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

How mapping List with array_agg ?

1 Answer 1

0

There's a problem with deserialization of your Interger[] column2 . Replace it with a Collection<Integer>

That should allow it to map succesfully.

Mapping array with Hibernate

Sign up to request clarification or add additional context in comments.

4 Comments

add Collection from java.util.Collection ?
correct, You'd have to import java.util.Collection. Does the linked question help?
Unfortunately no, a similar error. need to use Array or PgArray from import java.sql.Array;
whats the error when u use collection? You now have to convert the collection to an array whenever you're using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.