1

I have created new spring boot project with postgresql .I like to use posgressql array_agg(ex:get all department) using JPA Repository native query but its getting some error in blow posted. I have tried some alter solution but cant able to get expected data.

Error :

org.springframework.orm.jpa.JpaSystemException: No Dialect mapping for JDBC type: 2003; nested exception is org.hibernate.MappingException: No Dialect mapping for JDBC type: 2003

Expected : Should get array or list of data

@Repository
public interface PostGroupRepository extends JpaRepository<PostGroup, Integer> {

    @Query(value = "SELECT array_agg(department) FROM boxinfo;", nativeQuery = true)
    public Object[] getDept();
}

PostgreSQL Query

6
  • what things have you tried? As postgres returns datatype text[] - JPA /Hibernate will not have datatype for that. You need to do custom Data type mapping. Commented May 31, 2021 at 11:07
  • I just tried alternate queries only.if possible could give some example of custom data mapping @Lucia Commented May 31, 2021 at 11:09
  • vladmihalcea.com/postgresql-array-java-list You can check this Commented May 31, 2021 at 11:16
  • stackoverflow.com/questions/21630370/… check this as well Commented May 31, 2021 at 11:17
  • @Lucia is possible to get directly in native query itself??without adding additional works? Commented May 31, 2021 at 11:23

1 Answer 1

2

First solution is to use below dependency:

<dependency>
            <groupId>com.vladmihalcea</groupId>
            <artifactId>hibernate-types-52</artifactId>
            <version>2.11.1</version>
        </dependency>

he has custom types already written and register that in the custom dialect like below

public class CustomPostgreDialect extends PostgreSQL10Dialect {
    public CustomPostgreDialect() {
        super();
        this.registerHibernateType(2003, StringArrayType.class.getName());
    }
} 

And use this dialect as the hibernate dialect in application.yaml or application.properties of spring boot.

spring.jpa.properties.hibernate.dialect: <packageName>.CustomPostgreDialect

Second solution is to write the custom type yourself and register it in the dialect as shown above, if you don't want to use dependency.

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

Comments

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.