How to write Spring Data method name to retrieve all elements in a column?

10,253

Solution 1

In the Spring docs: http://static.springsource.org/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html there is nothing about getting only particular column/property from entity by query generated from method name. So I think that currently it is not possible.

Solution 2

The code you showed works/should work as expected. It's simply not causing the exception you see :).

The exception is referring to a Trade, which seems to indicate that you have a repository for Trade somewhere which seems to refer to a missing property. The code you've shown is definitely not the one causing the exception. This can effectively not be the case as you're defining the query manually so that the query derivation mechanism doesn't even kick in for the repo you've shown.

I've pushed a test case for you to see this in action.

Share:
10,253
Random42
Author by

Random42

Updated on June 26, 2022

Comments

  • Random42
    Random42 almost 2 years

    Suppose I have the class:

    @Entity
    public class Bean {
        @Id
        private String beanId;
        //other fields & setters and getters
    }
    

    And the corresponding Spring Data JPA repository, where I want to have in a List<String> all the beanIds.

    @RepositoryDefinition(domainClass = Bean.class, idClass = String.class)
    public interface BeanRepository {
        @Query("select b.beanId from Bean b")
        List<String> findAllBeanId();
    }
    

    As written above, everything works as expected; but this is a simple operation and I do not want to write a query explicitly. What should the name of the method be such that Spring Data can parse it and obtain the above mentioned query (or the same functionality). I have searched in both the reference documentation as two books I have on Spring Data. The above name (findAllBeanId) and others that I have tried (findBeanId, findBeanBeanId etc.) throw the following exception as root cause:

    org.springframework.data.mapping.PropertyReferenceException: No property find found for type Trade
        at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:353)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
        at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:271)
        at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:245)
        at org.springframework.data.repository.query.parser.Part.<init>(Part.java:72)
        at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)
        at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)
        at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:240)
        at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:68)
        at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:279)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:153)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:43)
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
        ... 22 more
    
  • Random42
    Random42 over 10 years
    I've searched into documentation and also in two books on Spring Data and didn't also found; however this doesn't necessarily means that it does not support it. It would be kind of weird to support more complex functionality and not support this rather simple one.
  • ragnor
    ragnor over 10 years
    It'll be a nice feature but IMHO not so easy to implement. Currently fetch method can return single entity or list of entities. With this extension any return type will be allowed but also should be verified against the selected property in entity. In case of selecting only one property/column the method signature is pretty but for two or more it will be Object[] or List<Object[]> that are nasty.
  • norgence
    norgence over 10 years
    It is. Projections are not supported in the query derivation mechanism currently but if you define the query manually you can actually use a query declaration like to original poster showed. The exception (s)he sees is not caused by the code (s)he shows, see my answer to the topic.
  • Random42
    Random42 over 10 years
    I have mentioned that the code works as expected: "As written above, everything works as expected". I was actually searching for a way of achieving the functionality without the annotation, but as I understand from the above answer and your comment it is not supported (I believe you are Oliver Gierke one of Spring Data authors so I take your word); thanks for the interest in offering support for the framework and keep up the good job!