p:datatable : getRowKey(T object) must be implemented by MYCLASS when basic rowKey algorithm is not used

UI Components for JSF
Post Reply
vished
Posts: 479
Joined: 02 Feb 2014, 17:38

17 Dec 2017, 19:36

Dear all,

I used LAZY - loading and my p:datable worked fine.
But now i would like to extend the p:datatable with selection and now I got the following error message:
java.lang.UnsupportedOperationException: getRowKey(T object) must be implemented by
MandatoryRequestController when basic rowKey algorithm is not used [component=mandatoryListForm:mandatoryListDatatable,view=/admin/mandatoryList.xhtml].

I have no idea what´s wrong because I´ve already implemented the getRowData(Object) and getRowKey(Object) into my bean:

Here my bean:

Code: Select all

@javax.faces.view.ViewScoped;
@Named
public class MandatoryRequestController extends LazyDataModel<Mandatory> implements SelectableDataModel<Mandatory>, Serializable {

	private static final long serialVersionUID = 2815796004558360299L;
	private static final Logger LOGGER = LoggerFactory.getLogger(MandatoryRequestController.class);

	@EJB
	private MandatoryService mandatoryService;

	private List<Mandatory> filteredMandatory;
	private List<Mandatory> selectedMandatoryList;
	private LazyDataModel<Mandatory> lazyModel;
	private List<Mandatory> list;

	@SuppressWarnings("serial")
	@PostConstruct
	public void findMandatoryByUser() throws NumberFormatException, MandatoryNotFoundException {

		lazyModel = null;

		if (lazyModel == null) {
			lazyModel = new LazyDataModel<Mandatory>() {
				
				@Override
				public List<Mandatory> load(int startingAt, int maxPerPage, String sortField, SortOrder sortOrder,
						Map<String, Object> filters) {

					try {
						list = mandatoryService.findAllMandatoryLazyLoading(null, startingAt, maxPerPage, sortField,
								sortOrder, filters);
						lazyModel.setRowCount(list.size());
					} catch (MandatoryNotFoundException e) {
						lazyModel.setRowCount(0);
					}
					return list;
				}

			};
		}
	}

	@Override
	public Mandatory getRowData(String rowKey) {
		for (Mandatory mandatory : list) {
			if (mandatory.getId().equals(rowKey))
				return mandatory;
		}
		return null;
	}

	@Override
	public Object getRowKey(Mandatory mandatory) {
		return mandatory;
	}

Here is my p:datatable:

Code: Select all

	<h:form id="mandatoryListForm">


						<p:dataTable id="mandatoryListDatatable"
							value="#{mandatoryRequestController.lazyModel}" var="mandatory"
							widgetVar="mandatoryTable"  multiViewState="true"
							resizableColumns="true"
							 selection="#{mandatoryRequestController.selectedMandatoryList}"
							filteredValue="#{mandatoryRequestController.filteredMandatory}"
							rows="50" pageLinks="10" paginatorPosition="bottom" lazy="true"
							paginator="true" reflow="true"
							currentPageReportTemplate="(Eintrag: {startRecord}-{endRecord} von {totalRecords}, Seite: {currentPage} von {totalPages})"
							paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
							rowsPerPageTemplate="10,20,50,100" paginatorAlwaysVisible="false"
							emptyMessage="Keine Firma gefunden">

							<f:facet name="header">

								<p:outputPanel style="float:left; margin-top: -3px">
									<p:menuButton value="Optionen">
										<p:menuitem value="Bearbeiten" disabled="#{mandatoryRequestController.selectedMandatoryList.size() != 0}"
											actionListener="#{menuView.update}" icon="fa fa-pencil" />
										<p:menuitem value="Löschen" actionListener="#{menuView.save}" disabled="#{mandatoryRequestController.selectedMandatoryList != null}"
											icon="fa fa-trash" />
									</p:menuButton>
								</p:outputPanel>

								<h:outputText value="Übersicht" />

								<p:outputPanel style="float:right; margin-top: -3px">
									<p:commandButton type="button" styleClass="Fs13 White"
										icon="fa fa-filter" />
									<p:commandButton id="toggler" type="button"
										styleClass="Fs13 White" icon="fa fa-list" />
									<p:columnToggler datasource="mandatoryListDatatable"
										trigger="toggler" />
								</p:outputPanel>
							</f:facet>
							
							<p:ajax event="rowSelect"  update=":mandatoryListForm:mandatoryListDatatable" />
        <p:ajax event="rowUnselect"  update=":mandatoryListForm:mandatoryListDatatable" />
        

							<p:column selectionMode="multiple"
								style="width:16px;text-align:center" toggleable="false" />

							<p:column sortBy="#{mandatory.companyName}"
								filterBy="#{mandatory.companyName}" filterStyle="display:none">
								<f:facet name="header">
									<h:outputText value="Firma" />
								</f:facet>

								<h:link outcome="/portal/mandatorySettingEdit">
									<h:outputText value="#{mandatory.companyName}" />
									<f:param name="id" value="#{mandatory.id}" />
								</h:link>
							</p:column>


							<p:column>
								<f:facet name="header">
									<h:outputText value="Optionen" />
								</f:facet>
								<p:splitButton value="Bearbeiten" icon="fa fa-pencil"
									actionListener="#{mandatoryAccountingEditController.doSave}"
									ajax="true">
									<p:menuitem value="Löschen"
										actionListener="#{mandatoryAccountingEditController.doSave}"
										update="growl" icon="fa fa-trash" />
									<p:menuitem value="Abrechnung erstellen"
										actionListener="#{mandatoryRequestController.doRedirectNewMandatoryAccounting(mandatory)}"
										update="growl" icon="fa fa-credit-card" />
								</p:splitButton>

							</p:column>



						</p:dataTable>
					</h:form>

My Entity:

Code: Select all

private Long id = NO_ID;

// CONSTRUCTOR
	public Mandatory() {
		super();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Mandatory other = (Mandatory) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	

	@Override
	public String toString() {
		return "Mandatory [id=" + id + "]";
	}
Where is the issue?
PF 8.0

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

19 Dec 2017, 10:08

You extend a lazy datamodel, but IN your bean you create (via new) an anonymous inner class. THAT one should implement the getRowKey

vished
Posts: 479
Joined: 02 Feb 2014, 17:38

26 Dec 2017, 02:26

Great, this was the issue - thank you very much
PF 8.0

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 36 guests