在primefaces中使用rowKey将dataTable与新实体一起使用

She*_*lly 5 datatable jsf primefaces

好吧,我知道我必须从我的对象中选择一个独特的字段,比如ID.然后我的rowKey是"item.id".这在dataTable中加载所有itens时工作正常,但是如果我添加一个新项(此对象在数据库中没有持久化)并且具有空ID,则它在我的dataTable中不会显示.

这是什么解决方案?我只是可以使用ID作为rowKey但有时我需要添加一个具有NULL ID的新对象(因为这还没有在数据库上保留).

看看我的代码:

<p:dataTable rowKey="#{item.id}" var="item"
                    value="#{orcamentoMB.itens}"
                    emptyMessage="Não foi encontrado nenhum registro"
                    id="dataTableItens"
                    selection="#{orcamentoMB.selectedItemOrcamento}"
                    selectionMode="single" rowIndexVar="rowIndex"
                    rowStyleClass="#{(rowIndex mod 2) eq 0 ? 'first-row' : 'second-row'}">
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 11

然后使用其他唯一标识符.也许hashCode()甚至是toString()

rowKey="#{not empty item.id ? item.id : item.hashCode()}"
Run Code Online (Sandbox Code Playgroud)