Spring Data Neo4j Extensions
Available extensions for Spring Data Neo4j repositories
Spring Data Neo4j 提供了一些可以添加到存储库的扩展或“混合”。什么是混合?根据 Wikipedia,混合是一种语言概念,允许程序员将一些代码注入到一个类中。混合编程是一种软件开发风格,其中在类中创建功能单元,然后将其与其他类进行混合。
Spring Data Neo4j offers a couple of extensions or "mixins" that can be added to repositories. What is a mixin? According to Wikipedia mixins are a language concept that allows a programmer to inject some code into a class. Mixin programming is a style of software development, in which units of functionality are created in a class and then mixed in with other classes.
Java 在语言级别不支持该概念,但我们通过一些接口和运行时模拟它,该运行时添加了适当的实现和拦截器。
Java does not support that concept on the language level, but we do emulate it via a couple of interfaces and a runtime that adds appropriate implementations and interceptors for.
默认添加的混合分别为 QueryByExampleExecutor
和 ReactiveQueryByExampleExecutor
。这些接口在 Query by Example 中进行了详细解释。
Mixins added by default are QueryByExampleExecutor
and ReactiveQueryByExampleExecutor
respectively. Those interfaces are
explained in detail in Query by Example.
提供的其他混合有:
Additional mixins provided are:
-
QuerydslPredicateExecutor
-
CypherdslConditionExecutor
-
CypherdslStatementExecutor
-
ReactiveQuerydslPredicateExecutor
-
ReactiveCypherdslConditionExecutor
-
ReactiveCypherdslStatementExecutor
Add dynamic conditions to generated queries
QuerydslPredicateExecutor
和 CypherdslConditionExecutor
都提供了相同概念:SDN 生成一个查询,您提供将被添加的“谓词”(Query DSL)或“条件”(Cypher DSL)。我们推荐 Cypher DSL,因为这是 SDN 本机使用的方法。您甚至可能需要考虑使用 annotation processor,它为您生成一个静态元模型。
Both the QuerydslPredicateExecutor
and CypherdslConditionExecutor
provide the same concept: SDN generates a query, you
provide "predicates" (Query DSL) or "conditions" (Cypher DSL) that will be added. We recommend the Cypher DSL, as this is
what SDN uses natively. You might even want to consider using the
annotation processor that generates
a static meta model for you.
这是如何工作的?按如上所述声明您的存储库,并添加 一个 以下接口:
How does that work? Declare your repository as described above and add one of the following interfaces:
Unresolved include directive in modules/ROOT/pages/repositories/sdn-extension.adoc - include::example$integration/imperative/QuerydslNeo4jPredicateExecutorIT.java[]
1 | Standard repository declaration |
2 | The Query DSL mixin |
或
OR
Unresolved include directive in modules/ROOT/pages/repositories/sdn-extension.adoc - include::example$integration/imperative/CypherdslConditionExecutorIT.java[]
1 | Standard repository declaration |
2 | The Cypher DSL mixin |
使用 Cypher DSL 条件执行器展示示例用法:
Exemplary usage is shown with the Cypher DSL condition executor:
Unresolved include directive in modules/ROOT/pages/repositories/sdn-extension.adoc - include::example$integration/imperative/CypherdslConditionExecutorIT.java[]
1 | Define a named Node object, targeting the root of the query |
2 | Derive some properties from it |
3 | Create an or condition. An anonymous parameter is used for the first name, a named parameter for
the last name. This is how you define parameters in those fragments and one of the advantages over the Query-DSL
mixin which can’t do that.
Literals can be expressed with Cypher.literalOf . |
4 | Define a SortItem from one of the properties |
代码与 Query-DSL 混合非常相似。使用 Query-DSL 混合的原因可以是对 API 比较熟悉,而且它也可以与其他存储一起使用。不采用它的原因是类路径上需要一个额外的库,它缺少遍历关系的支持,以及前面提到的它不支持谓词中的参数(从技术上来说它确实支持,但是没有 API 方法可以实际将它们传递到要执行的查询中)。
The code looks pretty similar for the Query-DSL mixin. Reasons for the Query-DSL mixin can be familiarity of the API and that it works with other stores, too. Reasons against it are the fact that you need an additional library on the class path, it’s missing support for traversing relationships and the above-mentioned fact that it doesn’t support parameters in its predicates (it technically does, but there are no API methods to actually pass them to the query being executed).
Using (dynamic) Cypher-DSL statements for entities and projections
添加相应的混合与使用 sdn-mixins.dynamic-conditions 没有什么不同:
Adding the corresponding mixin is not different from using the sdn-mixins.dynamic-conditions:
Unresolved include directive in modules/ROOT/pages/repositories/sdn-extension.adoc - include::example$integration/imperative/CypherdslStatementExecutorIT.java[]
扩展 ReactiveNeo4jRepository
时,请使用 ReactiveCypherdslStatementExecutor
。
Please use the ReactiveCypherdslStatementExecutor
when extending the ReactiveNeo4jRepository
.
CypherdslStatementExecutor
随 findOne
和 findAll
的多个重载一起提供。它们分别采用一个 Cypher-DSL 语句或此语句正在进行的定义作为第一个参数,如果是投影方法,则采用一个类型。
The CypherdslStatementExecutor
comes with several overloads for findOne
and findAll
. They all take a Cypher-DSL
statement respectively an ongoing definition of that as a first parameter and in case of the projecting methods, a type.
如果查询需要参数,则必须通过 Cypher-DSL 本身定义它们,并且还必须由它填充,如下面的清单所示:
If a query requires parameters, they must be defined via the Cypher-DSL itself and also populated by it, as the following listing shows:
Unresolved include directive in modules/ROOT/pages/repositories/sdn-extension.adoc - include::example$integration/imperative/CypherdslStatementExecutorIT.java[]
1 | The dynamic query is build in a type safe way in a helper method |
2 | We already saw this in sdn-mixins.dynamic-conditions, where we also defined some variables holding the model |
3 | We define an anonymous parameter, filled by the actual value of name , which was passed to the method |
4 | The statement returned from the helper method is used to find an entity |
5 | Or a projection. |
findAll
方法的作用与此类似。强制性 Cypher-DSL 语句执行器还提供了一个重载,返回分页结果。
The findAll
methods works similar.
The imperative Cypher-DSL statement executor also provides an overload returning paged results.