我正在尝试将Spring-Data-JPA集成到我的项目中。
有一件事让我困惑,就是如何通过注解实现setMaxResults(n)?
例如,我的代码:
public interface UserRepository extends CrudRepository<User , Long>
{
@Query(value="From User u where u.otherObj = ?1 ")
public User findByOtherObj(OtherObj otherObj);
}
我只需要从otherObj返回一个(仅一个)
用户,但是我找不到一种方法来注释maxResults。谁能给我一些提示?
(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中呢?如果我手动实现这个功能:
public class UserRepositoryImpl implements UserRepository
我必须在CrudRepository
中实现大量预定义的方法,这会很糟糕。
环境: spring-3.1, spring-data-jpa-1.0.3.RELEASE.jar, spring-data-commons-core-1.1.0.RELEASE.jar
List
作为该方法的返回类型,那么这就不是问题。 - Oliver Drotbohm