"h:selectonemenu generic converter for all entities without calling db again and again" Code Answer


my first question is, is there a generic converter for all type of entities?

this does indeed not exist in standard jsf. the jsf utility library omnifaces has such a converter in its assortiment, the omnifaces.selectitemsconverter. all you need to do is to declare it as converter of an uiselectone or uiselectmany component as follows:

<h:selectonemenu ... converter="omnifaces.selectitemsconverter">

see also the selectitemsconverter showcase. this converter relies on the tostring() of the object item. there's also another one, the omnifaces.selectitemsindexconverter, which relies instead on the index of the object item in the options list, see also the selectitemsindexconverter showcase.

there are currently no other jsf component/utility libraries offering the same.

second question is, is there a way to get selected object without any converter?

no. just use the omnifaces one so that you don't need to create a custom converter which hits the db. or if you want to go overboard, create a custom renderer for <h:selectonemenu> which renders the item index as option value and is able to set it as model value, but that's a lot of more work than a simple converter and you'd still need to do some additional work in order to get the desired object from the list based on the index — which just doesn't make any sense.

see also:

  • how to populate options of h:selectonemenu from database?
  • conversion error setting value for 'null converter'
  • implement converters for entities with java generics
By filbranden on January 17 2022

Answers related to “h:selectonemenu generic converter for all entities without calling db again and again”

Only authorized users can answer the Search term. Please sign in first, or register a free account.