【发布时间】:2012-07-22 07:31:24
【问题描述】:
我的 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 参考文档,但找不到任何关于通配符和 @query 的信息。
以下 hack 有效,但有点难看:
and lower(c.fullName) like '%' || lower(:name) || '%'
谁有更好的解决方案?
谢谢, 穆尔。
【问题讨论】:
-
一个稍微偏离主题的旁注,以防任何 Spring Data 作者是关于.. :) 也许这种特性(用通配符限制)可以添加到 @ 987654325@;例如
@Param(value="name", wildcardStategy=WildcardStrategy.BOTH)。诚然,我很少考虑这个问题,所以可能有强烈的反对意见!
标签: jpa-2.0 spring-data spring-data-jpa