尝试使用Spring Data JPA创build自定义存储库时找不到types错误的属性

我有一个媒体实体,有一些用户上传的文件的基本字段。 为了保存上传文件的字节数,我想创build一个自定义的存储库来保存这个function。 按照Spring文档中的步骤,我创build了一个如下所示的接口:

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接口的实现

有了这个,我创build了以下界面:

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

现在,当我启动服务器时,我得到以下堆栈跟踪:

 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 

我发现这个类似的post ,但那里的build议(都在同一个包,命名约定)是我已经做的事情。 我所有的媒体类和接口都在同一个包中,我使用的是“Impl”后缀。

有人可以请阐明为什么我得到这个错误,我怎么能解决它? 谢谢。

你写了:

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

你不可以。

MediaBytesRepository重命名为MediaRepositoryCustom

当然,您需要使用MediaRepositoryCustom名称来实现MediaRepositoryImpl

您必须将您的impl类命名为“InterfaceNameImpl”。 实现的默认后缀是Impl,我们可以根据需要改变它:

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

自定义接口的名称并不重要。