Coder Social home page Coder Social logo

cepr0 / hibernate-types-in-projection Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 5 KB

Bad attempt to use json or array types of hibernate-types lib in Spring Data projections

Java 100.00%
spring-data-jpa hibernate-types json array projection postgresql

hibernate-types-in-projection's Introduction

An attempt to use json/array types of hibernate-types in Spring Data projections

Projection:

public interface ParentProjection {
	String getName();
	List<String> getChildNames();
}

Query:

@Query(value = "" +
        "select " +
        "  p.name as name, " +
        "  array_agg(c.name) as childNames " +
        "from " +
        "  parents p " +
        "  join children c on c.parent_id = p.id " +
        "where " +
        "  p.id = ?1 " +
        "group by " +
        "  p.name" +
        "", nativeQuery = true)
Optional<ParentProjection> getParentWithChildNamesAsArray(Integer id);

Custom Postgres dialect:

public class PostgreSQLCustomDialect extends PostgreSQL95Dialect {

	public PostgreSQLCustomDialect() {
		super();
		registerHibernateType(Types.OTHER, JsonNodeStringType.class.getName());
		registerHibernateType(Types.ARRAY, StringArrayType.class.getName());
	}
}

When PostgreSQL aggregate function array_agg is used in the query above, then the following exception is raised:

org.springframework.orm.jpa.JpaSystemException: Could not instantiate Type: com.vladmihalcea.hibernate.type.array.StringArrayType;

If json_agg function is used, then we have another exception:

java.lang.IllegalArgumentException: Projection type must be an interface!  

hibernate-types-in-projection's People

Contributors

cepr0 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.