简体   繁体   English

Spring in Action第4版中有关JPA存储库示例的问题

[英]Questions for JPA repository example in Spring in Action 4th edition

Around Page 325: 在第325页周围:

SpitterRepository extends JpaRepository.
SpitterRepositoryImpl implements SpitterRepository. Also SpitterRepositoryImpl extends SpitterSweeper interface. 
    public interface SpitterSweeper{
        int eliteSweep();
    }

It says the eliteSweep method should be declared in the SpitterRepository interface. 它说eliteSweep方法应该在SpitterRepository接口中声明。 So do not quite understand it, why can't we only declare it in the eliteSweep interface? 因此,我不太了解它,为什么我们不能仅在eliteSweep接口中声明它?

from their doc : 从他们的文档

The repository proxy has two ways to derive a store-specific query from the method name. 存储库代理有两种从方法名称派生特定于商店的查询的方式。 It can derive the query from the method name directly, or by using an additionally created query. 它可以直接从方法名称或通过使用其他创建的查询派生查询。 Available options depend on the actual store. 可用选项取决于实际商店。 However, there's got to be an strategy that decides what actual query is created. 但是,必须有一种策略来确定要创建的实际查询。 Let's have a look at the available options. 让我们看一下可用的选项。

The query builder mechanism built into Spring Data repository infrastructure is useful for building constraining queries over entities of the repository. 内置在Spring Data存储库基础结构中的查询构建器机制对于在存储库实体上构建约束查询很有用。 The mechanism strips the prefixes find…By, read…By, and get…By from the method and starts parsing the rest of it. 该机制从方法中剥离前缀find ... By,read ... By和get ... By,然后开始解析其余部分。 The introducing clause can contain further expressions such as a Distinct to set a distinct flag on the query to be created. Introduction子句可以包含其他表达式,例如Distinct,以在要创建的查询上设置不同的标志。 However, the first By acts as delimiter to indicate the start of the actual criteria. 但是,第一个By用作分隔符,以指示实际标准的开始。 At a very basic level you can define conditions on entity properties and concatenate them with And and Or . 在最基本的级别上,您可以定义实体属性的条件,并将其与And和Or串联。

this means, that Spring Data JPA will construct query from method name and for simple queries, you don't need to write jpql/hql queries, spring data jpa will automatically create them. 这意味着,Spring Data JPA将根据方法名称构造查询,对于简单查询,您无需编写jpql/hql查询,Spring data jpa会自动创建它们。 Spring data jpa will parse method name and will build the needed query. Spring数据jpa将解析方法名称并构建所需的查询。

as for why are declared in interface? 至于why are declared in interface? the framework will try to create a repository proxy instance, concrete implementation of this interface which will be used where you inject this interface. 该框架将尝试创建一个存储库代理实例,此接口的具体实现将在您注入该接口的地方使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM