Spring Boot JPA Crud存储库 [英] Spring Boot JPA CrudRepository

查看:94
本文介绍了Spring Boot JPA Crud存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot + Spring Data JPA,并在尝试注入扩展CrudRepository的类时遇到此问题:

I'm working with Spring Boot + Spring Data JPA and facing this problem when trying to inject a class that extends CrudRepository:

由以下原因引起:org.springframework.beans.factory.BeanCreationException: 创建名称为'topicRepository'的bean时出错:无法解析 匹配的构造函数(提示:指定索引/类型/名称参数 避免类型歧义的简单参数)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'topicRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

存储库类:

public interface TopicRepository extends CrudRepository<Topic, Integer> {}

服务等级:

@Service
public class TopicService {

      @Autowired
      private TopicRepository topicRepository;
}

有什么建议吗?

推荐答案

我遇到了同样的问题,并且我通过切换Spring Boot版本来解决了这个问题.更改Spring Data JPA版本并没有执行任何操作(这是我假设的错误所在),因此我认为Spring Boot版本1.5.1中存在一个错误.我切换回1.4.3版,错误消失了.我没有尝试过后续版本/不同版本,因此您可能只需要试验依赖项及其版本.

I was having the same issue, and I fixed it by switching Spring Boot versions. Changing the Spring Data JPA versions did nothing (this is where I assumed the bug would be), so I think there is a bug in Spring Boot version 1.5.1. I switched back to version 1.4.3 and the error was gone. I didn't try subsequent/different versions, so you may just have to experiment with your dependencies and their versions.

为便于记录,您可以为服务类添加@Repository注释,这应该没有任何区别.我一直在使用service/dao模式以相同的方式设置这些应用程序,并且从来没有对注释过分挑剔.希望这可以帮助那些Spring Boot开发流程突然抛出错误的人!

For the record, you can have your service class annotated with @Repository, it shouldn't make any difference. I've been setting these apps up the same way using the service/dao pattern, and it has never been too picky with the annotations. Hopefully this may help others whose Spring Boot development flow suddenly throws an error!

这篇关于Spring Boot JPA Crud存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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