私は、PrimeFaces、Spring、および Hibernate を使用して JSF 2.1 プロジェクトに取り組んでいます。
JoinColumn によって Region と呼ばれるエンティティに関連する Provinces と呼ばれるエンティティがあります。
@Entity
@Table(name = "PROVINCE")
public class Province {
/* ... */
@ManyToOne
@JoinColumn(name = "ID_REGION",nullable=false)
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
@Column(name = "DESCRIPTION", nullable=false)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@Entity
@Table(name = "REGION")
public class Region {
/* ... */
@OneToMany(mappedBy = "region")
public List<Province> getProvince() {
return province;
}
public void setProvince(List<Province> province) {
this.province = province;
}
@Column(name = "DESCRIPTION", nullable=false)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
JSF ページにp:dataTable
、Province のリストを表示する があります。とりわけ、州が属する地域の名前を示す列があります。何かのようなもの:
<p:dataTable id="provincesDatatable"
value="#{provinceMB.lazyProvinceViewModel}"
var="province"
paginator="true" rows="15" lazy="true"
emptyMessage="Nothing to see here, move along"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
selection="#{admProvinceMB.selectedProvince}" selectionMode="single"
rowsPerPageTemplate="5,10,15">
<p:column headerText="Province Name"
sortBy="#{province.description}">
<h:outputText value="#{province.description}" />
</p:column>
<p:column headerText="Region Name"
sortBy="#{province.region}">
<h:outputText value="#{province.region.description}" />
</p:column>
</p:dataTable>
(バッキング Bean と LazyDataModel の実装は問題とは関係ないと思うので省略していますが、必要に応じて表示できます)。地域によるソートを除いて、これは正常に機能します。
上記の例は地域 ID で並べ替えますが、次のように地域の説明で並べ替えたいのですが、うまくいきません。
<p:column headerText="Region Name"
sortBy="#{province.region.description}">
<h:outputText value="#{province.region.description}" />
</p:column>
これを試すと、エラー org.hibernate.QueryException: could not resolve property: region.description of: my.package.entity.Province が表示されます
私が思いついた唯一の解決策は、関連付けられたリージョンの説明を返す一時フィールドをProvince
calledに追加するregionDescription
ことですが、これは非常に醜いハックです。この結果を得るより良い方法はありますか?
編集:問題は別の場所にありました。LazyDataModel の関連コードは次のとおりです。
public List<Province> findLazyProvince(int startingAt, int maxPerPage,
String sortField, SortOrder sortOrder, Map<String, String> filters)
{
Criteria crit = buildCriteria(filters);
if (sortField != null && !sortField.isEmpty()) {
if (sortOrder.equals(SortOrder.ASCENDING)) {
crit = crit.addOrder(Order.asc(sortField));
} else {
crit = crit.addOrder(Order.desc(sortField));
}
}
crit = crit.setFirstResult(startingAt).setMaxResults(maxPerPage);
return crit.list();
}