【问题标题】:Iterating over a java.util.Set inside <p:dataGrid>. causes, The class org.eclipse.persistence.indirection.IndirectSet doesn't have the property xxx迭代 <p:dataGrid> 内的 java.util.Set。原因,类 org.eclipse.persistence.indirection.IndirectSet 没有属性 xxx
【发布时间】:2014-04-27 19:54:45
【问题描述】:

我在 MySQL 数据库中有三个表。

  • 类别
  • 子类别
  • 产品

这些表之间的关系是直观的——按照它们出现的顺序一对多。


我从 MySQL 数据库中得到一个 List&lt;SubCategory&gt;,其中每个子类别都有一个 java.util.Set&lt;Product&gt;,显而易见。

我在&lt;p:dataGrid&gt; 中迭代List&lt;SubCategory&gt;,如下所示。

<p:dataGrid id="dataGrid" rows="4" first="0" columns="1" value="#{productDetailsManagedBean}" var="row" rowIndexVar="rowIndex" paginator="true" paginatorAlwaysVisible="false" pageLinks="10" lazy="true" rowsPerPageTemplate="1,2,3">

        <h:panelGrid columns="1" style="width:100%">
            <h:outputText value="#{row.subCatId}"/>

            <p:carousel id="carousel" value="#{row.productSet}" var="prodRow" numVisible="4" headerText="#{row.subCatName}">
                <h:outputText value="#{prodRow.prodId}"/>
            </p:carousel>

        </h:panelGrid>

</p:dataGrid>

&lt;p:carousel&gt; 的 value 属性不起作用。它只是说,{IndirectSet: not instantiated}。因此,使用#{prodRow.prodId} 之类的 EL 访问任何产品(如使用&lt;h:outputText&gt; 所见)都会引发类似的异常,

javax.el.PropertyNotFoundException: The class 'org.eclipse.persistence.indirection.IndirectSet' does not have the property 'prodId'

当我改变时,

<h:outputText value="#{prodRow.prodId}"/>

<h:outputText value="#{row.productSet.size()}"/>

正确显示row.productSet的大小(不为空,大小与List&lt;SubCategory&gt;中每个子类别的行数完全相同。Set被精确初始化)。

为什么在访问Product 的属性时会抛出异常,比如#{prodRow.prodId}

如何解决这个问题?

here 对此进行了精确解释,但在这种特殊情况下我无法解决相同的问题。

我使用的是 EclipseLink 2.3.2 提供的 JPA

【问题讨论】:

  • 看起来 PrimeFaces 的 Carousel 不喜欢 Set。您是否尝试过使用列表?
  • {IndirectSet: not instantiated} 表示您的方法正在 Set 上调用 toString。 IndirectSet 在尚未触发时返回此字符串而不是底层的 Set 实现,并且与您的问题无关,只是表明 p:carousel 没有按照您认为的方式使用 Set。建立关系渴望删除 Eclipselink 对 IndirectSet 的使用,看看会发生什么
  • @Chris : FetchType.EAGER 也不起作用。我现在到处都将Set 更改为List,它工作得很好,不需要使用.toArray(),如answer 所示。
  • 太棒了。使用 eager 的建议不是解决方案。这只是为了让您验证问题不在于间接集,而是所有集。很高兴你解决了。

标签: jsf jpa primefaces eclipselink


【解决方案1】:

我尝试将java.util.Set 更改为java.util.List,但这样做需要对应用程序进行太多更改,这很痛苦。

相反,我使用其toArray() 方法将java.util.Set 转换为一个数组,并且成功了。

<p:carousel id="carousel" value="#{row.productSet.toArray()}" var="prodRow" numVisible="6" headerText="#{row.subCatName}">
    <!--<h:outputText value="#{prodRow.prodId}"/>-->
    <p:graphicImage library="fileUpload" name="product_image/medium/#{prodRow.productImageSet.toArray()[0].prodImage}" alt="#{prodRow.prodName}" height="140" width="135"/>
</p:carousel>

我已将&lt;h:outputText&gt; 替换为&lt;p:graphicImage&gt;,以实现在&lt;p:graphicImage&gt; 上显示图像的具体要求。

我不知道问题是否与 JPA 提供程序 EclipseLink 本身或 java.util.Set 有关。如果有人知道,请澄清。


编辑:

我现在已将我的应用程序中的每个逆向(或映射)字段从java.util.Set 更改为java.util.List,并且效果很好。无需使用.toArray(),必须使用java.util.Set

【讨论】:

  • 建立关系 Eager to remove Eclipselink 使用 IndirectSet 看看会发生什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
相关资源
最近更新 更多