Mue*_*uel 10 jpa-2.0 spring-data spring-data-jpa
我已经定义了我的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);
}
Run Code Online (Sandbox Code Playgroud)
以上是完美的工作,生成的SQL就是我自己编写的.问题是我想在:name参数中添加周围的通配符.有什么好办法吗?我已经看过Spring Data JPA参考文档了,我找不到任何关于wildards和@query.
以下hack有效,但有点难看:
and lower(c.fullName) like '%' || lower(:name) || '%'
Run Code Online (Sandbox Code Playgroud)
有没有人有更好的解决方案?
谢谢,穆尔.
我也不认为这是一种 hack——它正是针对这些问题定义 JPQL 语法的方式。
但是,还有一种使用CriteriaBuilder/CriteriaQuery 的替代方案,但也许您会发现它更复杂(但您会得到编译时类型安全性作为回报)。
| 归档时间: |
|
| 查看次数: |
2670 次 |
| 最近记录: |