Search code examples
jsfautocompleteprimefacesconverterspojo

primefaces autocomplete, how to convert a list of objects from a database query


I have my autocomplete component connecting to my managed bean which then connects to my service layer and gets list from the database. I am trying to reference the showcase and any other thing I find online, but I can not get my autocomplete to work. I had a cast exception, so it looked like I needed a converter. But I am having trouble writing the converter. I'm confused on how everything is passed along.

        <p:autoComplete id="placesSearchBar" value="#{searchBarBean.place}" completeMethod="#{searchBarBean.findSimilarPlaces}"
                        var="Place" itemLabel="#{Place.name}" itemValue="#{Place}" converter="PlaceConverter" />

        public class SearchBarBean 
    {
        private Place place;
        private SearchQueryService searchQueryService;
        private Criteria criteria;

        /**
         * @return the place
         */
        public Place getPlace() {
            return place;
        }

        /**
         * @param place the place to set
         */
        public void setPlace(Place place) {
            this.place = place;
        }

        /**
         * @return the searchQueryService
         */
        public SearchQueryService getSearchQueryService() {
            return searchQueryService;
        }

        /**
         * @param searchQueryService the searchQueryService to set
         */
        public void setSearchQueryService(SearchQueryService searchQueryService) {
            this.searchQueryService = searchQueryService;
        }

        /**
         * @return the criteria
         */
        public Criteria getCriteria() {
            return criteria;
        }

        /**
         * @param criteria the criteria to set
         */
        public void setCriteria(Criteria criteria) {
            this.criteria = criteria;
        }

        public List<Place> findSimilarPlaces(String query)
        {
            getCriteria().setName(query);
            List<Place> places = getSearchQueryService().findPlaces(criteria);
            return places;
        }
    }

    public class PlaceConverter implements Converter
{
    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String submittedValue) {
        SearchBarBean searchBarBean = new SearchBarBean();
        return null;
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
         return null;
    }
}

Solution

  • You converter must do two things: convert a string (which gets submitted by your page) into an instance of your pojo and convert an instance of you pojo into a string so that it can be displayed on your page.

    You could achieve that by returning the primary key for place in your getAsString method. Then, in your getAsObject method, you would get that string containing the primary key for the selected item and query the database for the associated record. For that to work you'd have to have access to your query service from the converter. Give that a shot and let me know how it goes.