使用Spring Data JPA,JpaRepository和CrudRepository之间究竟有什么区别?

And*_*ili 8 spring spring-data spring-data-jpa

我正在开发一个Spring Boot应用程序,它使用Spring Data JPA(在Hibernate 4上)来访问我的数据库.

我的疑问与DAO接口有关(JPA用它来自动生成查询).

所以,在我的项目中,我有这两个接口:

1)住宿DAO:

@Repository
@Transactional(propagation = Propagation.MANDATORY)
public interface AccomodationDAO extends JpaRepository<Accomodation, Long> {

    Accomodation findById(@Param("id") Long id);

}
Run Code Online (Sandbox Code Playgroud)

2)EventDAO:

public interface EventDAO extends CrudRepository<Event, Integer> {

    public Event findByLocation(Point location);

    public Event findById(@Param("id") Integer id);

}
Run Code Online (Sandbox Code Playgroud)

它们都工作正常并使用相同的逻辑来声明查询.

我唯一的疑问是:第一个扩展JpaRepository而第二个实现CrudRepository.

JpaRepositoryCrudRepository之间究竟有什么区别?什么是最好的选择使用或在什么情况下更好地使用一个而不是另一个选择?

另一个疑问是:为什么我定义的DAO接口扩展了JpaRepositoryCrudRepository本身就是接口?据我所知,接口是实现的而不是扩展的...我缺少什么?

Rob*_*roj 8

请注意,JpaRepository扩展了CrudRepository.比较这两个接口的JavaDoc:

JpaRepository CrudRepository

简而言之 JpaRepository

  • 具有其他JPA特定方法,支持例如示例查询,批量删除,手动刷新对数据库的更改
  • 查询方法返回List而不是 Iterable's

如果您使用的是JPA,则应使用JpaRepository.


归档时间:

查看次数:

15817 次

最近记录:

9 年,3 月 前