我想查询数据库以返回一个人员列表,从第二个字母开始按名称排序。
Bailey
Adam
甚至可能吗?
答案 0 :(得分:2)
假设JPA 2.0,您可以使用自定义JPQL查询执行此操作:
@Query("select p from Person p order by substring(p.name, 2)")
List<Person> findPersons();
Hibernate支持SUBSTRING
JPQL函数。引用Hibernate文档:
<强> SUBSTRING 强>
提取字符串值的一部分。
substring( string_expression, numeric_expression [, numeric_expression] )
第二个参数表示起始位置。第三个(可选)参数表示长度。
请注意,String的第一个位置是1.如果未指定长度,则默认为String的其余部分的长度。这在JPA 2.0 specification第4.6.17.2.1段中指定。
答案 1 :(得分:1)
见full list of supported query keywords。
您可能应该使用ENDING_WITH
,LIKE
或REGEX
。
示例:
public interface UserRepository extends JpaRepository<User, Long> {
User findByNameEndingWith(String nameEndingWith);
}
findByNameEndingWith("dam"); //Will return you entity with name Adam