まず、タスクは@NodeEntity
、 @RelationshipEntity
、および@Relationship
と追加パラメーターに基づいて、データクラスのサイファー要求を生成することでした。 クエリジェネレーターが作成されましたが、クエリ文字列は実行時に計算され、既存の@Query
アノテーションのパラメーターとして使用できなかったため、新しい@CustomQuery
アノテーションが追加され、そのプロセッサーは記述されたクエリジェネレーターを使用しました。
使用されているソフトウェアのバージョン:
spring-data-neo4j-5.0.9.RELEASE
neo4j-3.4.6
neo4j-ogm-3.1.4
Spring Data Neo4jにカスタムクエリジェネレーターを追加する
@CustomQuery
アノテーションなどでマークされたneo4jリポジトリメソッドのサイファーリクエストを生成する独自のメカニズムを追加するには、 CustomNeo4jRepositoryFactory extends Neo4jRepositoryFactory
をCustomNeo4jRepositoryFactory extends Neo4jRepositoryFactory
するCustomNeo4jRepositoryFactory extends Neo4jRepositoryFactory
を作成し、その中のgetQueryLookupStrategy
メソッドを再定義@CustomQuery
必要があります;新しいクエリCustomGraphQueryLookupStrategy extends GraphQueryLookupStrategy
返す
@Override protected Optional<QueryLookupStrategy> getQueryLookupStrategy(QueryLookupStrategy.Key key, EvaluationContextProvider evaluationContextProvider) { return Optional.of(new CustomGraphQueryLookupStrategy(session)); }
また、標準のNeo4jRepositoryFactoryBean
クラスNeo4jRepositoryFactoryBean
新しいCustomNeo4jRepositoryFactoryBean
クラスNeo4jRepositoryFactoryBean
拡張し、 createRepositoryFactory
メソッドをオーバーライドする必要もあります。 新しいリポジトリファクトリのインスタンスを返す必要があります。
@Override protected RepositoryFactorySupport createRepositoryFactory(Session session) { return new CustomNeo4jRepositoryFactory(session); }
Spring Data Neo4jが使用するリポジトリファクトリBeanを理解するには、構成内の@EnableNeo4jRepositories
アノテーションで明示的に指定する必要があります。
@EnableNeo4jRepositories(..., repositoryFactoryBeanClass = CustomNeo4jRepositoryFactoryBean.class)
追加のCustomQuery
クエリアノテーションがCustomQuery
さCustomQuery
。 リポジトリメソッドがこのアノテーションでマークされている場合、オーバーライドresolveQuery
メソッドのCustomGraphRepositoryQuery extends GraphRepositoryQuery
、リクエストのオブジェクトを返しますCustomGraphRepositoryQuery extends GraphRepositoryQuery
:
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory, NamedQueries namedQueries) { if (method.isAnnotationPresent(CustomQuery.class)) { GraphQueryMethod queryMethod = new GraphQueryMethod(method, metadata, factory); return new CustomGraphRepositoryQuery(queryMethod, session, method.getAnnotation(CustomQuery.class)); } else { return super.resolveQuery(method, metadata, factory, namedQueries); } }
CustomGraphRepositoryQuery
はQuery
オブジェクトを返すgetQuery
メソッドを実装します;そのコンストラクターは、 CustomQuery
アノテーションとこのアノテーションでマークされたMethod
オブジェクトからのデータに基づいて、 CustomGraphRepositoryQuery
で構築された暗号クエリーを受け入れます。
@Override protected Query getQuery(Object[] parameters) { return new Query(query, resolveParams(parameters)); }
resolveParametres(Object[])
メソッドとそれによって使用されるresolveParametres(Object[])
はGraphRepositoryQuery
でプライベートであるため、単にCustomGraphRepositoryQuery
コピーされCustomGraphRepositoryQuery
(リフレクションを使用できます。クエリ生成は実行前に発生するため、これはパフォーマンスに影響しません)。
おわりに
したがって、必要に応じて、サイファー要求を生成する独自のメカニズムを宣言できます。
次の記事では、クエリジェネレータ自体、そのパラメータ、作業のメカニズム、発生した問題、およびそれらの解決策について説明します。