开发者

How to iterate and dynamically parameterize JPQL queries?

开发者 https://www.devze.com 2023-03-15 16:57 出处:网络
I have a page with simple ID as a URL param. What I do now is run a query to return a number of associated entities that basically need to get iterated over: I need a schedule of games to be returned

I have a page with simple ID as a URL param. What I do now is run a query to return a number of associated entities that basically need to get iterated over: I need a schedule of games to be returned for LEAGUE games, CUP games, and PLAYOFFS games, so the result lists must differ per iteration.

Each of these schedules gets its own tab in the GUI. I already need JSTL c:forEach for the RichFaces tabs, so I "only" need to find a way to set another WHERE restriction onto a component (here a Seam EntityQuery sub class instance).

The problem here is: How do you parameterize each query with the current entity during iteration? How is it best done in Seam/JBoss EL? How do I get another restriction or two into an EntityQuery instance during iteration?

Here's the JSF code I use:

<rich:tabPanel>
  <c:forEach items="#{participationListQuery.resultList}" var="pa">
    <rich:tab label="#{...}" switchType="client">
      <h:form>
      <rich:dataTable id="schedule-scores"
                      value="#{rosterScheduleQuery.resultList}"
                      var="sgl"
                      width="100%"
                      rows="20">
        ...
      </rich:dataTable>
      </h:form>
    </rich:tab>
  </c:forEach>
</rich:tabPanel>

The problem here is you can't simply c开发者_JAVA百科all something like #{rosterScheduleQuery.setCustomRestriction(pa.group.round.subCompetition.competition.name)} in the c:forEach as this expression will only be evaluated once (if I understand correctly). I might be missing the overall point here, as this is all rather procedural.

How do you generally solve iteration and runtime-parameterized queries (additional WHERE conditions)? Best practices are always welcome.

Thanks

Edit: I'm already using Facelets, but rich:tabPanel requires JSTL c:forEach to be used. See http://relation.to/11633.lace.


JBossEL allows method calls with parameters, so you could add an intermediate bean that has a method that takes the type and retrieves the results. Basically replace "#{rosterScheduleQuery.resultList}" with "#{someNewBean.getResults(pa)}". This bean would have the query injected into it, which it could parameterize as needed. Probably not as elegant as you would like, but I think this is how I would do it.


It is not a good idea to mix both JSP and JSF handlers. You are using switchType equal To client which means its content will be prerendered to the client, and no interaction with the server will happen during switching.

Well, it can not be the best solution, but i think it can help you

<rich:tabPanel value="#{participationTabPanel}">
</rich:tabPanel>

You managed bean

@Name
public class ParticipationManagedForm {

    private @In EntityQuery<Participation> participationListQuery;

    @Factory(value="participationTabPanel", scope=ScopeType.EVENT)
    public HtmlTabPanel getParticipationTabPanel() {
        HtmlTabPanel panel = new HtmlTabPanel();

        for(List<Participation> participationList: participationListQuery.getResultList()) {
            HtmlTab htmlTab = new HtmlTab();

            // Set up htmlTab right here

            panel.getChildren().add(htmlTab);
        }

        return panel;
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消