How to use Primefaces autocomplete with POJO list and POJO property as select value

In our current project, we want to replace <h:selectOneMenu>

with Primefaces <p:autocomplete>

. The select elements are Pojos list (JPA objects). The difference from the given examples in the examples shows that we want the primary key property (id) of the object to be selected as a selection value, so it can easily be passed as a parameter of the form:

    <f:viewParam name="playerId" value="#{playerPreRenderViewListener.searchCriteria.playerId}" />
    <f:viewParam name="year" value="#{playerPreRenderViewListener.searchCriteria.year}" />
    <h:inputText value="#{playerPreRenderViewListener.searchCriteria.year}"/>
    <p:autoComplete var="player" itemLabel="#{}" itemValue="#{}"
        completeMethod="#{playerBean.completePlayer}" forceSelection="true"
    <h:commandButton value="Submit" action="showTeam?faces-redirect=true&amp;includeViewParams=true" />


Unfortunately, the above example will result in a PropertyNotFoundException:

itemLabel = "# {}": property 'name' not found on type java.lang.Long '

The problem is that the attribute var

is of type Long

, not Player

. When used simple, <h:selectMenu>

it works in combination with <f:selectItems>


<f:selectItems var="player" value="#{playerBean.listPlayers}" itemLabel="#{}" itemValue="#{}" />


Does anyone know how to deal with this problem?


source to share

1 answer

You can add a backing bean method that returns the player that belongs to the currently active playerId

one and set this attribute currentPlayer

to the value of your backing bean:

public Player getCurrentPlayer() {
  // find player by playerId and return


And in the view:

<p:autoComplete var="player" itemLabel="#{}" itemValue="#{player}"
        completeMethod="#{playerBean.completePlayer}" forceSelection="true"




All Articles