【发布时间】:2014-04-27 19:54:45
【问题描述】:
我在 MySQL 数据库中有三个表。
- 类别
- 子类别
- 产品
这些表之间的关系是直观的——按照它们出现的顺序一对多。
我从 MySQL 数据库中得到一个 List<SubCategory>,其中每个子类别都有一个 java.util.Set<Product>,显而易见。
我在<p:dataGrid> 中迭代List<SubCategory>,如下所示。
<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>
<p:carousel> 的 value 属性不起作用。它只是说,{IndirectSet: not instantiated}。因此,使用#{prodRow.prodId} 之类的 EL 访问任何产品(如使用<h:outputText> 所见)都会引发类似的异常,
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<SubCategory>中每个子类别的行数完全相同。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