Spring-Data-JPA注释的setMaxResults? [英] setMaxResults for Spring-Data-JPA annotation?

查看:160
本文介绍了Spring-Data-JPA注释的setMaxResults?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Spring-Data-JPA合并到我的项目中。
有一件事让我感到困惑的是如何通过注释实现setMaxResults(n)?

I am trying to incorporate Spring-Data-JPA to my project. One thing confuses me is how to achieve setMaxResults(n) by annotation ?

例如,我的代码:

public interface UserRepository extends CrudRepository<User , Long>
{
  @Query(value="From User u where u.otherObj = ?1 ")
  public User findByOhterObj(OtherObj otherObj);
}

我只需要返回一个(只有一个) )来自otherObj的用户,但我找不到一种方法来注释maxResults ...有人可以给我一个提示吗?

I only need to return one (and only one) User from otherObj , but I cannot find a way to annotate the maxResults ... Can somebody give me a hint ?

(mysql抱怨) :

(mysql complains :

com.mysql.jdbc.JDBC4PreparedStatement@5add5415: select user0_.id as id100_, user0_.created as created100_ from User user0_ where user0_.id=2 limit ** NOT SPECIFIED **
WARN  util.JDBCExceptionReporter - SQL Error: 0, SQLState: 07001
ERROR util.JDBCExceptionReporter - No value specified for parameter 2

我找到了一个链接:https://jira.springsource.org/browse/DATAJPA-147
我试过但失败了。现在似乎不可能?
为什么这么重要的功能没有内置在Spring-Data中?

I found a link : https://jira.springsource.org/browse/DATAJPA-147 , I tried but failed. It seems not possible now ? Why such a important feature not built in Spring-Data ?

如果我手动实现这个功能:

If I implement this feature manually :

public class UserRepositoryImpl implements UserRepository

我必须实现这将是非常糟糕的 CrudRepository 中的大量预定义方法。

I have to implement tons of predefined methods in CrudRepository , this will be terrible.

环境:spring-3.1,spring-data- jpa-1.0.3.RELEASE.jar,spring-data-commons-core-1.1.0.RELEASE.jar

environments : spring-3.1 , spring-data-jpa-1.0.3.RELEASE.jar , spring-data-commons-core-1.1.0.RELEASE.jar

推荐答案

截至Spring Data JPA 1.7.0(Evans发布列车)。



...您可以使用新推出的 Top 第一个关键字,允许您定义这样的查询方法:

As of Spring Data JPA 1.7.0 (Evans release train).

… you can use the newly introduced Top and First keywords that allow you to define query methods like these:

findTop10ByLastnameOrderByFirstnameAsc(String lastname);

Spring Data会自动将结果限制为您定义的数字(如果省略则默认为1)。注意,结果的排序在这里变得相关(通过 OrderBy 子句,如示例中所示,或者通过递交 Sort 参数进入方法)。在博客文章中阅读更多内容,包括 Spring Data Evans发布系列的新功能文档

Spring Data will automatically limit the results to the number you defined (defaulting to 1 if omitted). Note, that the ordering of the results becomes relevant here (either through an OrderBy clause as seen in the example or by handing a Sort parameter into the method). Read more on that in the blog post covering new features of the Spring Data Evans release train or in the documentation.

仅检索数据片段Spring Data使用分页抽象,它在请求方提供 Pageable 接口,以及 Page 事物结果方面的抽象。所以你可以从

To retrieve only slices of data Spring Data uses the pagination abstraction which comes with a Pageable interface on the requesting side as well as a Page abstraction on the result side of things. So you could start with

public interface UserRepository extends Repository<User, Long> {

  List<User> findByUsername(String username, Pageable pageable);
}

并像这样使用:

Pageable topTen = new PageRequest(0, 10);
List<User> result = repository.findByUsername("Matthews", topTen);

如果您需要知道结果的上下文(实际上是哪一页?是第一个一个?总共有多少?)使用 Page 作为返回类型:

If you need to know the context of the result (which page is it actually? is it the first one? how many are there in total?) use Page as return type:

public interface UserRepository extends Repository<User, Long> {

  Page<User> findByUsername(String username, Pageable pageable);
}

客户端代码可以执行以下操作:

The client code can then do something like this:

Pageable topTen = new PageRequest(0, 10);
Page<User> result = repository.findByUsername("Matthews", topTen);
Assert.assertThat(result.isFirstPage(), is(true));

如果您使用<$,我们不会触发要执行的实际查询的计数投影c $ c> Page 作为返回类型,因为我们需要找出总计有多少元素来计算元数据。除此之外,请确保您实际上为 PageRequest 配备了排序信息以获得稳定的结果。否则,您可能会触发查询两次并获得不同的结果,即使数据未在下面更改。

Not that we will trigger a count projection of the actual query to be executed in case you use Page as return type as we need to find out how many elements there are in total to calculate the metadata. Beyond that be sure you actually equip the PageRequest with sorting information to get stable results. Otherwise you might trigger the query twice and get different results even without the data having changed underneath.

这篇关于Spring-Data-JPA注释的setMaxResults?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆