2

对于这个问题,我还有另一种情况,即我有一个扩展基本实体的模型,并且该基本实体具有属性 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

4

1 回答 1

12

哦,对不起,我太笨了,问题是 id 为空。我忘记了我对这些值进行了硬编码。所以无论如何,对于将来遇到同样问题的人来说,要使用较少的代码行键,请确保您设置了以下数据表属性:1.) rowKey 2.) selection 3.) selectionMode

还要确保 rowKey 属性不为空。

于 2012-10-24T01:58:02.490 回答