https://github.com/cepr0/hibernate-types-in-projection
Bad attempt to use json or array types of hibernate-types lib in Spring Data projections
https://github.com/cepr0/hibernate-types-in-projection
array hibernate-types json postgresql projection spring-data-jpa
Last synced: 3 months ago
JSON representation
Bad attempt to use json or array types of hibernate-types lib in Spring Data projections
- Host: GitHub
- URL: https://github.com/cepr0/hibernate-types-in-projection
- Owner: Cepr0
- Created: 2018-12-10T20:45:03.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-10T21:03:09.000Z (over 7 years ago)
- Last Synced: 2025-09-26T15:34:56.660Z (9 months ago)
- Topics: array, hibernate-types, json, postgresql, projection, spring-data-jpa
- Language: Java
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
An attempt to use json/array types of [hibernate-types](https://github.com/vladmihalcea/hibernate-types) in Spring Data projections
Projection:
```java
public interface ParentProjection {
String getName();
List getChildNames();
}
```
Query:
```java
@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 getParentWithChildNamesAsArray(Integer id);
```
[Custom Postgres dialect](https://vladmihalcea.com/hibernate-no-dialect-mapping-for-jdbc-type/):
```java
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!