我有另一个案例就是这个问题,我有一个模型,它扩展了一个基本实体,并且该基本实体具有属性id。我使用该id作为rowKey,它会抛出此错误。当我将rowKey的值设置为模型(而不是抽象基础)中的任何属性时,数据表可以正常工作。
请注意,我正在使用JavaEE6。
模特:
@Entity
@SequenceGenerator(name = "ID_GENERATOR", sequenceName = "USER_ADDRESS_SEQ")
public class UserAddress extends BaseEntity { //.. }
@MappedSuperclass
public abstract class BaseEntity implements Serializable, IEntity {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "ID_GENERATOR")
@Column(name = "ID")
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
} ..
JavaEE6 bean:
@Stateless
@Named
public class UserAddressBean implements Serializable {
private static final long serialVersionUID = -6104153017102665096L;
private List<UserAddress> addresses;
private UserAddress address;
public List<UserAddress> getAddresses() {
addresses = new ArrayList<UserAddress>();
UserAddress temp = new UserAddress();
temp.setDescription("test");
addresses.add(temp);
temp = new UserAddress();
temp.setDescription("test");
addresses.add(temp);
return addresses;
}
public UserAddress getAddress() {
return address;
}
public void setAddress(UserAddress address) {
this.address = address;
}..
和xhtml页面:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
template="/shared/page/_oneColumn.xhtml">
<ui:define name="content">
<h:form id="form">
<p:panel>
<f:facet name="header"></f:facet>
<p:dataTable id="addresses" var="address"
value="#{userAddressBean.addresses}" rowKey="#{address.id}"
selection="#{userAddressBean.address}" selectionMode="single">
<p:column headerText="#{msg['field.description']}">
<h:outputText value="#{address.description}" />
</p:column>
</p:dataTable>
<f:facet name="footer"></f:facet>
</p:panel>
</h:form>
</ui:define>
</ui:composition>
对这个问题有任何想法吗?
谢谢,
czetsuya
答案 0 :(得分:12)
哦对不起,我是如此愚蠢,问题是id为null。我忘记了我对这些值进行了硬编码。因此,对于将遇到相同问题的未来人员而言,要使用较少的代码行键,请确保设置了以下数据表属性: 1.)rowKey 2.)选择 3.)selectionMode
还要确保rowKey属性不为null。