如何迭代和动态参数化 JPQL 查询?

发布于 2024-11-16 19:13:48 字数 1335 浏览 3 评论 0原文

我有一个页面,其中包含简单的 ID 作为 URL 参数。我现在要做的是运行一个查询来返回一些基本上需要迭代的关联实体:我需要为 LEAGUE 游戏、CUP 游戏和 PLAYOFFS 游戏返回一个游戏时间表,因此结果列表必须有所不同迭代。

每个计划在 GUI 中都有自己的选项卡。我已经需要 JSTL c:forEach 作为 RichFaces 选项卡,所以我“只”需要找到一种方法来对组件(这里是 Seam EntityQuery 子类实例)设置另一个 WHERE 限制。

这里的问题是:如何在迭代期间用当前实体参数化每个查询?在 Seam/JBoss EL 中如何最好地完成它?如何在迭代期间将另一个或两个限制添加到 EntityQuery 实例中?

这是我使用的 JSF 代码:

<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>

这里的问题是您不能简单地在 c:forEach 中调用类似 #{rosterScheduleQuery.setCustomRestriction(pa.group.round.subCompetition.competition.name)} 的内容因为这个表达式只会被评估一次(如果我理解正确的话)。我可能错过了这里的总体要点,因为这都是相当程序化的。

您通常如何解决迭代和运行时参数化查询(附加 WHERE 条件)?最佳实践始终受到欢迎。

谢谢

编辑:我已经在使用 Facelets,但是 rich:tabPanel 需要使用 JSTL c:forEach 。请参阅http://relation.to/11633.lace

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 call 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老娘不死你永远是小三 2024-11-23 19:13:48

JBossEL 允许带参数的方法调用,因此您可以添加一个中间 bean,该 bean 具有采用该类型并检索结果的方法。基本上将“#{rosterScheduleQuery.resultList}”替换为“#{someNewBean.getResults(pa)}”。该 bean 会将查询注入其中,并可根据需要对其进行参数化。可能不像你想要的那么优雅,但我想这就是我会做的。

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.

一曲爱恨情仇 2024-11-23 19:13:48

混合使用 JSP 和 JSF 处理程序并不是一个好主意。您使用的 switchType 等于 client,这意味着其内容将预呈现给客户端,并且在切换期间不会与服务器发生交互

好吧,它可能不是最好的解决方案,但我认为它可以帮助你

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

你托管 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;
    }

}

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;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文