Spring Data Neo4jでのカスタムクエリジェネレーターの作成(パート1)

まず、タスクは@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 Neo4jRepositoryFactoryCustomNeo4jRepositoryFactory 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クエリアノテーションがCustomQueryCustomQuery 。 リポジトリメソッドがこのアノテーションでマークされている場合、オーバーライド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); } } 

CustomGraphRepositoryQueryQueryオブジェクトを返すgetQueryメソッドを実装します;そのコンストラクターは、 CustomQueryアノテーションとこのアノテーションでマークされたMethodオブジェクトからのデータに基づいて、 CustomGraphRepositoryQueryで構築された暗号クエリーを受け入れます。


 @Override protected Query getQuery(Object[] parameters) { return new Query(query, resolveParams(parameters)); } 

resolveParametres(Object[])メソッドとそれによって使用されるresolveParametres(Object[])GraphRepositoryQueryでプライベートであるため、単にCustomGraphRepositoryQueryコピーされCustomGraphRepositoryQuery (リフレクションを使用できます。クエリ生成は実行前に発生するため、これはパフォーマンスに影響しません)。


おわりに


したがって、必要に応じて、サイファー要求を生成する独自のメカニズムを宣言できます。


次の記事では、クエリジェネレータ自体、そのパラメータ、作業のメカニズム、発生した問題、およびそれらの解決策について説明します。



Source: https://habr.com/ru/post/J434902/


All Articles