Multiple queries in the same view


I would like to run multiple queries then show results in a page such as :

I create a first controller query :

// Showing results as a EbeanList
  public CompletionStage<Result> index() {
    return SearchSomething().thenApplyAsync((List<LocationExtractedData> infos) -> {                   
          return ok(views.html.sitemap.index.render(  Sysuser.findByUserName(request().username()), infos)  );
            }, httpExecutionContext.current());
  public CompletionStage<List<LocationExtractedData>> SearchSomething() {
    return CompletableFuture.supplyAsync(() -> {
		return db.withConnection(
              connection -> {
				// Imagines this is a complexe QUERY (Later in the future...)
				final String sql =  "SELECT sysuser_id, role_id "
				                   +"from sysuser_role "
								   +"where sysuser_id = '1' "
								   +"and role_id in ('1','2','3','4','5') ";
				final RawSql rawSql = RawSqlBuilder.parse(sql).create();				
				Query<LocationExtractedData> query = Ebean.find(LocationExtractedData.class);  
                List<LocationExtractedData> list = query.findList();  
				return list;				              				
		}, httpExecutionContext.current());

Can you telling me how to run multiple and optimized queries in the same time for my page full of dashboards, charts and tables!

If i create multiple list of ebeanLists ( queries ), does this will affect the loading of my page ?
IF not, then, what should i do ?

Thank you in advance,

Hi @placebo86,

you have two options:

  1. each of the dashboard query is mapped to a separate Play action so a single client sends multiple requests (one per widget in the dashboard) and starts painting each widget as soon as the response is delivered
  2. invoke multiple CompletableFuture.supplyAsync(() -> {return db.withConnection( ... (one per query) and compose the results using thenCombine's in the CompletableFuture's API.

The former has a nicer UX since users stat to see data as soon as it’s available but produces a lot of HTTP requests.
The later will not render any information until all views completed.


Thank you for your response,
What do you think about this which works good for me : Using play with existing database ( models )

However, i don’t know how to combine more than 2 completionstage, can you help please in my controller to send more list of data (EbeanList) to my view , thank you so much

Hi @placebo86,

I thiunk Baeldung does a great job at explaining exactly that so I won’t try to be half as good as them.