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

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

问题描述

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

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

例如,我的代码:

public interface UserRepository extends CrudRepository<User , Long>
{
  @Query(value="From User u where u.otherObj = ?1 ")
  public User findByOtherObj(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 抱怨:

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 is such an important feature not built into 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 would 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 版本系列).

您可以使用新引入的 TopFirst 关键字来定义这样的查询方法:

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 this:

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));

如果您使用 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天全站免登陆