尝试使用 Spring Data JPA 创建自定义存储库时找不到类型错误的属性 [英] No property found for type error when try to create custom repository with Spring Data JPA

查看:18
本文介绍了尝试使用 Spring Data JPA 创建自定义存储库时找不到类型错误的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Media 实体,其中包含用户上传文件的一些基本字段.为了保存上传文件的字节数,我想创建一个包含该功能的自定义存储库.遵循 Spring 文档,我创建了一个如下所示的界面:

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;
}

然后我为这个接口提供了一个名为MediaBytesRepositoryImpl

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

我发现了这个类似的帖子,但那里的建议(都在同一个包中,命名约定)是我已经在做的事情.我所有的媒体类和接口都在同一个包中,我使用的是Impl"后缀.

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.

推荐答案

您写道:

那里的建议(都在同一个包中,命名约定)是我已经在做的事情.

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

不,你没有.

将您的 MediaBytesRepository 重命名为 MediaRepositoryCustom.

Rename you MediaBytesRepository to MediaRepositoryCustom.

当然,您还需要一个名为 MediaRepositoryImplMediaRepositoryCustom 实现.

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

这篇关于尝试使用 Spring Data JPA 创建自定义存储库时找不到类型错误的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆