原创

リストを挿入する方法Spring Data JPAの非INネイティブクエリにパラメータをテキスト配列として追加しますか?

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

JPA の params インジェクションについて本当に理解できないことがあります。SQLで文字列のリストを表す明白な方法であるため、SQLではanyが(または)になると@Query予想されます。List<String>{...,...}::TEXT::VARCHAR

@Query(
    """
    SELECT ... FROM ...
    WHERE sql_function(:myParam)
                       ^^^ I expect that to be injected as '{a,b,c,etc}'
    """,
    nativeQuery = true,
)
fun findAll(myParam: List<String>): List<...>

手動書き込みチェック

jsonb_contains_any(jsonb_data jsonb, collection_path text[], collection_key text, search_values text[])は私が作成したカスタム PostgreSQL 関数です。厳密にテスト実行されており、手動で引き続き動作します。

SELECT * FROM logbook_reports
WHERE log_type = 'PNO' AND jsonb_contains_any(value, '{catchOnboard}', 'species', NULL);
SELECT * FROM logbook_reports
WHERE log_type = 'PNO' AND jsonb_contains_any(value, '{catchOnboard}', 'species', '{}');
SELECT * FROM logbook_reports
WHERE log_type = 'PNO' AND jsonb_contains_any(value, '{catchOnboard}', 'species', '{HKE}');

Successfully run. Total query runtime: 258 msec.
1 rows affected.

最初の試み

最初は、これがそのまま使えるのではないかと期待していました。

@Query(
    """
    SELECT *
    FROM logbook_reports
    WHERE log_type = 'PNO'
      AND jsonb_contains_any(value, '{catchOnboard}', 'species', (:specyCodes))
    """,
    nativeQuery = true,
)
fun findAll(specyCodes: List<String>): List<LogbookReportEntity>

リクエスト後specyCodes=['HKE']、次のエラーが発生します。

24-04-11 19:39:58.973 DEBUG o.s.w.f.CommonsRequestLoggingFilter      : After request [GET /bff/v1/prior_notifications?specyCodes=HKE]
24-04-11 19:39:58.974 ERROR o.a.j.l.DirectJDKLog                     : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessResourceUsageException: JDBC exception executing SQL [
        SELECT *
        FROM logbook_reports
        WHERE log_type = 'PNO' AND jsonb_contains_any(value, '{catchOnboard}', 'species', (?))
        ] [ERROR: function jsonb_contains_any(jsonb, unknown, unknown, character varying) does not exist
  Hint: No function matches the given name and argument types. You might need to add explicit type casts.
  Position: 83] [n/a]; SQL [n/a]] with root cause

'species'ミナ型であることさえ「理解」していないことに気づくだろうTEXT他にありますでしょうか?

2回目の試み

次に、すべてのパラメータをキャストしようとしました。

@Query(
    """
    SELECT *
    FROM logbook_reports
    WHERE log_type = 'PNO'
      AND jsonb_contains_any(value, ARRAY['catchOnboard'], CAST('species' AS TEXT), CAST(:specyCodes AS TEXT[]))
    """,
    nativeQuery = true,
)
fun findAll(specyCodes: List<String>): List<LogbookReportEntity>

同じリクエスト (specyCodes=['HKE']):

24-04-11 19:48:02.601 DEBUG o.s.w.f.CommonsRequestLoggingFilter      : After request [GET /bff/v1/prior_notifications?specyCodes=HKE]
24-04-11 19:48:02.605 ERROR o.a.j.l.DirectJDKLog                     : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.DataIntegrityViolationException: JDBC exception executing SQL [
        SELECT *
        FROM logbook_reports
        WHERE jsonb_contains_any(value, ARRAY['catchOnboard'], CAST('species' AS TEXT), CAST((?) AS TEXT[]))
        ] [ERROR: malformed array literal: "HKE"
  Detail: Array value must start with "{" or dimension information.] [n/a]; SQL [n/a]] with root cause

'HKE'単純な文字列であるかのように注入するだけです...

正文到此结束
热门推荐
本文目录