在Spring中实现JpaRepostiory时使用存储库批注 [英] Using repository annotation when implementing JpaRepostiory in Spring

查看:99
本文介绍了在Spring中实现JpaRepostiory时使用存储库批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否理解正确,因此请澄清一下. 如果要为我的实体创建存储库,例如:

I'm not sure if I understand it correctly so want to clarify. If I want to create a repository for my entity eg.:

public interface BookRepository extends JpaRepository<Book, Id> {}

我应该用@Repository对其进行注释吗?根据此问题,@ Repository注释可将SQL对持久性的支持,但是JpaRepostiory已经不这样做了吗?最佳做法是-注释还是不注释?

Should I annotate it with @Repository? According to this question @Repository annotation translates exceptions from SQL to persistence ones but doesn't JpaRepostiory already do that? What's the best practice - to annotate or not?

推荐答案

使用JpaRepository时,无需使用@Repository

这只是一个接口,具体实现由Spring作为代理对象动态创建,并在那里处理JDBC异常.

It is just an interface and the concrete implementation is created dynamically as a proxy object by Spring and the JDBC Exceptions are handled there.

创建自定义DAO时需要使用@Repository,以便spring创建一个bean并正确处理异常.

You need to use @Repository when you create a Custom DAO, so that spring creates a bean and handles the exception properly.

这篇关于在Spring中实现JpaRepostiory时使用存储库批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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