简体   繁体   中英

How to limit the MySQL Query with Ebean in Play Framework

Heys guys, i have a Problem using Play Framework. I am trying to display a large amount of Data (from this Database). When i am using the "find.all()" the Play Framework Server crashes, since it takes to much memory.

I got a DB Model named:

@Entity
public class dblp_pub_new extends Model {
[...]
    public dblp_pub_new() {}

    public static List<dblp_pub_new> all() {
        return find.all();
    }

    public String getDoi() {
        return doi;
    }

    public void setMdate() {
        this.mdate = new Date();
    }

    public static Finder<String,dblp_pub_new> find = new Finder<String, dblp_pub_new>(String.class, dblp_pub_new.class);

}

My rendering function is, which is contained in Application.java:

public static Result dois(){
    return ok(views.html.index.render(dblp_pub_new.all(), DoiForm));
}

I am trying to limit the all() Query to 50 (best would be per page). But i cant seem to figure it out. I think i need a List returned to display it on the webpage. But I can't get it to work. I would be really relieved if one of you guys (and girls) could help me figuring out this problem. I've tried it with "fetch" and "setMaxRows()" but I only get errors i can't seem to solve. If there is something unclear, please just ask and i will try to provide as much information as i can. Thank you.

Using setMaxRows() should work. Try this:

dblp_pub_new.find.setMaxRows(50).findList()

By the way, you should name your class according to the Java convention : DblpPubNew . It will make your code easier to read.

Ebean has a helper for making pagination of data easier. It's called a PagingList there. Play's Finder helper class for Ebeans allows you to get such a PagingList for your query .

Say you want to display 50 items per page, and want to retrieve the items for the first page. Then you'd write something like this

public static List<dblp_pub_new> getpageItems(int page) {
    int pageSize = 50;
    return find.findPagingList(pageSize).getPage(page).getList();
}

Also, please note that your class name dblp_pub_new is highly unusual. Refer to this question for more information.

in addition to Carsten's answer I only mention that Page itself contains several useful methods which help ie building pagination menu in the view, so it's easier to use whole Page object:

public static com.avaje.ebean.Page<dblp_pub_new> getPage(int pageSize, int page) {
    return find.findPagingList(pageSize).getPage(page);
}

and then you can use it in the view like:

<ul>
  @for(item <- myPage.getList){
    <li>@item.name</li>
  }
</ul>

<b>Total page count is: @myPage.getTotalPageCount()</b>

The currently accepted answer suggesting findPagingList is not valid anymore. See https://github.com/ebean-orm/ebean/issues/96

Now using setMaxRows() should work with any Query but you have to use findPagedList on that (vs. findPagingList ). Something like this:

List<dblp_pub_new> list = dblp_pub_new.find.query()
   .where().eq("some_column", some_value)
   .orderBy("some_column desc")
   .setMaxRows(50)
   .findPagedList()
   .findList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM