Java JPQL 限制查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20679237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:33:30  来源:igfitidea点击:

JPQL limit query

javajpa-2.0limitjpql

提问by user3115056

How can I limit in a select query of JPQL named query? I need the limit to be done in the query level itself and not in the java layer!!! I am trying to use

如何限制 JPQL 命名查询的选择查询?我需要在查询级别本身而不是在java层中完成限制!!!我正在尝试使用

@NamedQueries(value = {
        @NamedQuery(name = UserNotification.QueryName.NOTIFICATION_DISPLAYED,
                    query = "SELECT un FROM UserNotification un " +
                            "WHERE un.orgId IN (:orgList) " +
                            "AND un.user.id = :userId LIMIT 5")

but in vain!!!

但徒劳无功!!!

Please suggest

请建议

采纳答案by Kevin Bowersox

JPQLdoes not provide a mechanism to limit queries. This is most often achieved by using the setMaxResults()method on the Query. If you must avoid specifying this in Java code, you could make a view in the database that contains your query and performs the limit. Then map an entity to this view as you would a table.

JPQL不提供限制查询的机制。这通常是通过使用 上的setMaxResults()方法来实现的Query。如果您必须避免在 Java 代码中指定此项,您可以在包含您的查询的数据库中创建一个视图并执行限制。然后像映射表一样将实体映射到此视图。

Example:

例子:

List<String> resultList= query.setMaxResults(100).getResultList();

回答by Udit Agarwal

while executing the query with entity manager just write .setMaxResults(no of obj)

在使用实体管理器执行查询时,只需编写 .setMaxResults(no of obj)

回答by muttonUp

For specific @NamedQueries where a limit is required, you can switch to @NamedNativeQuery

对于需要限制的特定@NamedQueries,您可以切换到@NamedNativeQuery

@NamedNativeQuery(
  name=UserNotification.QueryName.NOTIFICATION_DISPLAYED_LIMIT5,
  query="SELECT un.* FROM user_notification un " + 
        "WHERE un.user.id = ?1 LIMIT 5",
  resultClass=UserNotification.class
)

Not quite as smooth, but does the job.

不太顺利,但可以完成工作。

回答by George Siggouroglou

In case you are working with spring-datayou should use the Pageable Interface. A sample code below,

如果您正在使用spring-data,您应该使用Pageable Interface。下面的示例代码,

My Service,

我的服务,

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

@Service
public class MyModelService {

    @Autowired
    private MyModelRepository myModelRepository;

    @Transactional
    public Page<MyModel> findMyModelTop5() {
        return myModelRepository.findMyModelTop5(new PageRequest(0, 5));
    }
}

My Repository,

我的存储库,

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

@Repository
public interface MyModelRepository extends JpaRepository<MyModel, Integer> {

    @Query("SELECT mm FROM MyModel mm")
    public Page<MyModel> findMyModelTop5(Pageable pageable);

}

You can find a more complete answer about the spring data available options here.

您可以在此处找到有关弹簧数据可用选项的更完整答案。