jpa-2.0 – 是否可以为@Query参数添加通配符?
发布时间:2020-12-14 17:46:16 所属栏目:Java 来源:网络整理
导读:我定义了我的ContactDao如下: public interface ContactDao extends JpaRepositoryContact,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 LD
我定义了我的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的内容. 以下hack的作品,但是有点丑陋: and lower(c.fullName) like '%' || lower(:name) || '%' 有没有人有更好的解决方案? 谢谢, 解决方法
我也不会把它称之为黑客 – 这是为这些问题定义了JPQL语法的方式.
然而,有一个替代使用CriteriaBuilder/CriteriaQuery,但也许你发现它更复杂(但你得到编译时类型的安全性作为回报). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |