5

Having a hard time figuring out if i'm hitting a bug or doing something stupid...

Spring Boot v2.0.0.M7, spring-data-jpa, spring-data-rest, MySQL

The following @Query

@Query("select DISTINCT item.statusCode from Item item")
public List<String> lookupStatusCodes();

on a PagingAndSortingRepository is throwing a

java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class java.lang.String!
at org.springframework.data.mapping.context.PersistentEntities.lambda$getRequiredPersistentEntity$2(PersistentEntities.java:78) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at java.util.Optional.orElseThrow(Unknown Source) ~[na:1.8.0_151]
at org.springframework.data.mapping.context.PersistentEntities.getRequiredPersistentEntity(PersistentEntities.java:77) ~[spring-data-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:72) ~[spring-data-rest-webmvc-3.0.2.RELEASE.jar:3.0.2.RELEASE]

statusCode is a varchar and Item itself works as expected as an @Entity, but trying to project to a List of Strings (or List<Object>) fails with the above.

If it matters, I intentionally don't want to return a Page<String> here (no need to have paging since the expected resultset is small).

jamey graham
  • 1,194
  • 1
  • 10
  • 16
  • You cannot get List you can only get List from Repository – pvpkiran Jan 18 '18 at 14:37
  • i don't believe this is true - Spring Data examples include returning projections from repository queries – jamey graham Jan 18 '18 at 15:56
  • thats correct. But In your question, or code you havent mentioned anything related to using projections. – pvpkiran Jan 18 '18 at 16:26
  • i'm not using projections - i just meant that Repos aren't limited to returning T types (which is how i read your comment). See my comment below that Projections aren't working either though – jamey graham Jan 18 '18 at 16:31
  • @jameygraham have you found something about it? I used this before Spring 2, and it was working. I cannot make it works anymore (same exception: Couldn't find PersistenceEntity...). I cannot find any example in the [documentation of the latest version][https://docs.spring.io/spring-data/data-commons/docs/2.2.x/reference/html/#repositories] When I debug Spring data repository code, it looks like it cannot work anymore (it looks for a mapping in "persistentEntities" of AbstractMappingContext, but only the entities of the repositories are there). Maybe it is not possible anymore? – Cyril Gambis Feb 19 '19 at 14:06
  • Sorry that i don't have a good answer, but i never directly solved the problem. If you check my response to [this Qs duplicate](https://stackoverflow.com/a/48327489/3456359) you'll see how i chose to work around this (basically avoid returning types that aren't PersistentEntities from @Queries) – jamey graham Apr 19 '19 at 11:08
  • not a duplicate of the mentioned question. – jwenting Aug 30 '19 at 10:53

1 Answers1

1

A String cannot be mapped directly. You will need a mapper object. Create a model class with a string field -

package org.xyz.model;

import java.io.Serializable;

public class StringResult implements Serializable {

    private static final long serialVersionUID = 1L;
    private String result;

    public StringResult(
        String result) {
        super();
        this.result = result;
    }

    public String getResult() {
        return result;
    }
}

Then change the query to use the model class -

@Query("select new org.xyz.model.StringResult(DISTINCT item.statusCode as result) from Item item")
public List<StringResult> lookupStatusCodes();
harsh tibrewal
  • 785
  • 6
  • 21
  • 2
    i understand your reasoning why you thought this would work (and i too think it _should_ work), but it actually generates the same error (just for the Result class vs. String). It _should_ actually be unnecessary to wrap a String. Digging in a bit, it looks like Projections also aren't working (same error), so it's starting to feel like i'm just hitting a bug. – jamey graham Jan 18 '18 at 16:29
  • 1
    @jameygraham did you find any solution for this? having the same for spring-boot 2.0.3, spring-data Lovelace-M3 – Nik Handyman Jun 27 '18 at 16:12
  • @jameygraham https://github.com/spring-projects/spring-boot/issues/10312 and https://github.com/spring-projects/spring-data-commons/blob/master/src/main/java/org/springframework/data/mapping/context/MappingContext.java yes, it's hitting an exception for unmappable entities which should never include String obviously. Feels like a bug in Spring Boot ever since 2.0 (and still there and not fixed in the latest release). – jwenting Aug 30 '19 at 10:50