No property found for type error when try to create custom repository with Spring Data JPA

43,576

Solution 1

You wrote:

the suggestions there (all in same package, naming convention) are things I'm already doing.

No you do not.

Rename you MediaBytesRepository to MediaRepositoryCustom.

And of course you need an implementation of MediaRepositoryCustom with the name MediaRepositoryImpl.

Solution 2

You must name your impl class as "InterfaceNameImpl". Default postfix for implementation is Impl, we can change it as we like:

<repositories base-package="com.acme.repository" repository-impl-postfix="FooBar" />

The name of custom interfaces does not matter.

Share:
43,576
dnc253
Author by

dnc253

Updated on October 10, 2020

Comments

  • dnc253
    dnc253 over 3 years

    I have a Media entity that has some basic fields for files uploaded by the user. For saving the bytes of the files uploaded, I want to create a custom repository that holds that functionality. Following the steps in the Spring documentation, I've created an interface that looks like this:

    public interface MediaBytesRepository
    {
        public byte[] getBytes(Media media) throws IOException;
        public void saveBytes(Media media, byte[] bytes) throws IOException;
        public void appendBytes(Media media, byte[] bytes) throws IOException;
        public void deleteBytes(Media media) throws IOException;
        public boolean bytesExist(Media media) throws IOException;
    }
    

    Then I provided an implementation for this interface called MediaBytesRepositoryImpl

    With this, I then created the following interface:

    public interface MediaRepository extends JpaRepository<Media, Long>, MediaBytesRepository
    {
    }
    

    Now, when I start up the server, I get the following stack trace:

    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mediaRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract void com.foo.bar.core.media.MediaBytesRepository.saveBytes(com.foo.bar.core.media.Media,byte[]) throws java.io.IOException!
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    .....
    Caused by: java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract void com.foo.bar.core.media.MediaBytesRepository.saveBytes(com.foo.bar.core.media.Media,byte[]) throws java.io.IOException!
        at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:92)
        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:280)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
        at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
        at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
        ... 20 more
     Caused by: java.lang.IllegalArgumentException: No property save found for type class com.foo.bar.core.media.Media
        at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:73)
        at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:92)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:319)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:333)
        at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:301)
        at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:265)
        at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:239)
        at org.springframework.data.repository.query.parser.Part.<init>(Part.java:70)
        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)
        ... 27 more
    

    I found this similar post, but the suggestions there (all in same package, naming convention) are things I'm already doing. All my media classes and interfaces are in the same package, and I'm using the "Impl" suffix.

    Can someone please shed some light on why I'm getting this error and how I can fix it? Thanks.

  • dnc253
    dnc253 over 11 years
    Looks like I misunderstood how things are supposed to be named. After your answer I looked at the documentation again and realized that the name of my implementing class needs to be the name of the Spring Data interface + "Impl" and not the name of my interface + "Impl". So, I was able to keep the name of my interface MediaBytesRepository and I just had to name the implementation MediaRepositoryImpl instead of MediaBytesRepositoryImpl. Thanks for the help.
  • Mike R
    Mike R almost 10 years
    Made the same mistake... To summarize it should be: 1) YourInterfaceName 2) YourInterfaceNameCustom 3) YourInterfaceNameImpl
  • prashanth-g
    prashanth-g about 7 years
    You saved my day. Working like charm
  • Martin Erlic
    Martin Erlic almost 7 years
    Is this in the pom?
  • Saurabh
    Saurabh about 5 years
    @Ralph We have defined consistency level while building Cluster as default when application starts. If we implement custom repository with different consistency level, will this override that?
  • Ralph
    Ralph about 5 years
    @Saurabh: please raise a new question (and I do not know it)
  • Saurabh
    Saurabh about 5 years
    @Ralph, Thanks. I raised stackoverflow.com/questions/55859442/…