Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用Spring数据JPA创建自定义存储库时,未找到类型错误的属性_Java_Spring_Repository_Spring Data Jpa - Fatal编程技术网

Java 尝试使用Spring数据JPA创建自定义存储库时,未找到类型错误的属性

Java 尝试使用Spring数据JPA创建自定义存储库时,未找到类型错误的属性,java,spring,repository,spring-data-jpa,Java,Spring,Repository,Spring Data Jpa,我有一个媒体实体,它有一些用户上传文件的基本字段。为了保存上传文件的字节,我想创建一个保存该功能的自定义存储库。按照中的步骤,我创建了如下界面: public interface MediaBytesRepository { public byte[] getBytes(Media media) throws IOException; public void saveBytes(Media media, byte[] bytes) throws IOException;

我有一个媒体实体,它有一些用户上传文件的基本字段。为了保存上传文件的字节,我想创建一个保存该功能的自定义存储库。按照中的步骤,我创建了如下界面:

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

然后,我创建了以下界面:

public interface MediaRepository extends JpaRepository<Media, Long>, MediaBytesRepository
{
}
公共接口MediaRepository扩展了JpaRepository、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
严重:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例时发生异常
org.springframework.beans.factory.BeanCreationException:创建名为“mediaRepository”的bean时出错:FactoryBean在创建对象时引发异常;嵌套异常为java.lang.IllegalArgumentException:无法为方法public abstract void com.foo.bar.core.media.MediaBytesRepository.saveBytes(com.foo.bar.core.media.media,byte[])创建查询元模型抛出java.io.IOException!
位于org.springframework.beans.factory.support.FactoryBeanRegistrySupport.dogetObject fromFactoryBean(FactoryBeanRegistrySupport.java:149)
.....
原因:java.lang.IllegalArgumentException:无法为方法public abstract void com.foo.bar.core.media.MediaBytesRepository.saveBytes(com.foo.bar.core.media.media,byte[])创建查询元模型引发java.io.IOException!
位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:92)
位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
位于org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor。(RepositoryFactorySupport.java:280)
位于org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148)
位于org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
位于org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
位于org.springframework.beans.factory.support.FactoryBeanRegistrySupport.dogetObject fromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 20多
原因:java.lang.IllegalArgumentException:未找到类型类com.foo.bar.core.media.media的属性保存
位于org.springframework.data.mapping.PropertyPath.(PropertyPath.java:73)
位于org.springframework.data.mapping.PropertyPath.(PropertyPath.java:92)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:319)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:333)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:301)
位于org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:265)
位于org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:239)
位于org.springframework.data.repository.query.parser.Part.(Part.java:70)
位于org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:180)
位于org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)
位于org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:240)
位于org.springframework.data.repository.query.parser.PartTree(PartTree.java:68)
位于org.springframework.data.jpa.repository.query.PartTreeJpaQuery(PartTreeJpaQuery.java:57)
位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)
... 还有27个
我发现了这个,但是那里的建议(都在同一个包中,命名约定)是我已经在做的事情。我所有的媒体类和接口都在同一个包中,我使用的是“Impl”后缀

有人能告诉我为什么会出现这个错误,以及我如何修复它吗?谢谢。

你写道:

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

不,你没有

将您的
MediaBytesRepository
重命名为
MediaRepositoryCustom


当然,您需要一个名为
MediaRepositoryImpl

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

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


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

看起来我误解了事物的名称。在您回答之后,我再次查看了文档,并意识到实现类的名称需要是Spring数据接口的名称+“Impl”,而不是接口的名称+“Impl”。因此,我能够保留接口的名称
MediaBytesRepository
,我只需将实现命名为
MediaRepositoryImpl
,而不是
MediaBytesRepositoryImpl
。谢谢你的帮助。犯了同样的错误。。。总而言之,这应该是:1)你的界面名称2)你的界面名称3)你的界面名称你救了我一天。像charm@Ralph在应用程序启动时,我们将构建集群时的一致性级别定义为默认值。如果我们使用不同的一致性级别实现自定义存储库,这会覆盖它吗?@Saurabh:请提出一个新问题(我不知道),这是pom中的吗?