11

ContactDaoを次のように定義しました。

public interface ContactDao extends JpaRepository<Contact, Long> {

  /**
   * Finds all contacts that the given user has entered where the contact's full name matches {@code name}.
   * @param userId The current user's LDAP id.
   * @param name The name to search for.
   * @return A list of contacts matching the specified criteria.
   */
  @Query(" select c from Form as f" +
           " inner join f.contacts as c" +
           " where f.requestorUserId = :userId" +
           " and lower(c.fullName) like lower(:name)" +
           " order by lower(c.fullName)")
  List<Contact> findUserContactsByUserIdAndName(@Param("userId") String userId, @Param("name") String name);

}

上記は完全に機能しており、生成されたSQLは私が自分で書いたものです。:name問題は、パラメータに周囲のワイルドカードを追加したいということです。これを行うための良い方法はありますか?Spring Data JPAリファレンスドキュメントを確認しましたが、wildardsとについては何も見つかりません@query

次のハックは機能しますが、少し醜いです。

and lower(c.fullName) like '%' || lower(:name) || '%'

誰かがより良い解決策を持っていますか?

ありがとう、ムエル。

4

1 に答える 1

3

私はそれをハックとは呼びません - これは、まさにこれらの問題に対して JPQL 構文が定義されている方法です。

ただし、 CriteriaBuilder/CriteriaQueryを使用する代替手段がありますが、さらに複雑になる可能性があります (ただし、代わりにコンパイル時の型の安全性が得られます)。

于 2013-02-28T17:10:06.427 に答える