我如何使用@OneToMany集合进行分页 [英] How can I do paging with @OneToMany collections

查看:684
本文介绍了我如何使用@OneToMany集合进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Post实体和一个Comment实体以及一对多的关系:

pre $ @ $ c $ @Entity class Post {
...
@OneToMany
List< Comment>注释;
...
}

如何实现这样的分页:

 发帖= //找到帖子。 
返回post.getComments()。fetch(100,10); //找到第11页(页面大小10);

是否可以使用@OneToMany集合在JPA,
或我们是否必须完全重写JPA的关联机制? (例如,创建一个可以管理分页,排序和搜索的PersistentList集合类型)。



P.S .:我最近发现了Play!框架在JPA之上使用了一个非常有趣的lib:Siena。 Siena非常易于使用,并且在JPA / Hibernate之上是一个很好的抽象。但我无法找到如何对其关联进行分页。



更新:

Play框架有一个查询类似于Django的语法:

  Post.findAll()。from(100).fetch(10); //分页

其中

  Post.findAll()

将返回一个JPAQuery对象,一个自定义查询键入Play。



但与关联的集合,例如:

  Post.comments 

只会返回一个List,它不支持分页或其他查询。

  Post.comments 

也会返回一个JPAQuery对象或类似对象,那么你可以在query集合上查询:

  Post.comments.from(100).fetch(10); 

或者插入一个新的评论,而不会实际提取任何评论:

  Post.comments.add(新评论(...)); 

我的第一个想法是,我们可以创建List的子类,然后Post类变为:

  @Entity class Post {
...
@OneToMany
QueryList< Comment>注释;
...
}

和QueryList将具有fetch(),from ()方法,这对JPAQuery是间接的。



但我不知道Hibernate / JPA是否会识别它,或干扰它。


是否有可能在JPA(...)

$ b之上使用@OneToMany集合来模拟动态分页? $ b

不支持。标准方法是使用JPQL查询来检索给定帖子的注释,并使用 Query#setFirstResult(int) Query# setMaxResults(int)


在我第一次想到的时候,我们可以创建一个List的子类, )。但是我不知道Hibernate / JPA是否会识别它,或者干扰它。


显然,如果没有 heavy 修补程序彻底改变默认行为。


Suppose I have a Post entity and a Comment entity and a one to many relationship:

@Entity class Post {
    ...
    @OneToMany
    List<Comment> comments;
    ...
}

How can I achieve paging like this:

Post post = //Find the post.
return post.getComments().fetch(100, 10); // Find the 11th page (page size 10);

Is it possible to emulate dynamic paging with @OneToMany collections on top of JPA, or do we have to rewrite the association mechanism of JPA totally ? (e.g. create a PersistentList collection type that could manage the paging, sorting and searching).

P.S.: I recently found the Play! framework uses a very interesting lib on top of JPA: Siena. Siena is very easy to use, and is a good abstraction on top of JPA/Hibernate. But I can't find how to do paging with its associations.

Update:

Play framework has a query syntax similar to Django:

Post.findAll().from(100).fetch(10);  // paging

where

Post.findAll() 

will return a JPAQuery object, a customized query type in Play.

But with associated collections, e.g.:

Post.comments

will just return a List, which doesn't support paging or other queries.

I was wondering how to extend it, so that

Post.comments

will also return a JPAQuery object or similar, then you can query on the "query" collection:

Post.comments.from(100).fetch(10);

or insert a new Comment without actually fetching any of the comments:

Post.comments.add(new Comment(...));

On my first thought, we could create a subclass of List, then the Post class would become:

@Entity class Post {
    ...
    @OneToMany
    QueryList<Comment> comments;
    ...
}

and QueryList will have fetch(), from() methods that indirect to JPAQuery's.

But I don't know whether Hibernate/JPA will recognize this, or interfere with it.

解决方案

Is it possible to emulate dynamic paging with @OneToMany collections on top of JPA (...)

Not supported. The standard approach would be to use a JPQL query to retrieve the comments for a given post and and to use Query#setFirstResult(int) and Query#setMaxResults(int).

On my first thought, we could create a subclass of List, (...). But I don't know whether Hibernate/JPA will recognize this, or interfere with it.

It obviously won't without an heavy patch to drastically change the default behavior.

这篇关于我如何使用@OneToMany集合进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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