I am trying to make a custom JSF converter for my category model. The value I get from the field and to the converter is an category id. Should I retrieve the category by that ID, and return that object? I have tried injecting an entitymanager but that did not work, since it is not managed. I was able to find a post where a guy did it, but I don’t know if it is usual to inject entitymanagers into converters.
Or should I override the toString method in the Category class, and return a string such as
2, "long description"
and then use the toString as value in the fields. When I want it back to an object I split it and use the setters on the category object. I have not worked with converters before so I don’t know if this is any better?
Not being able to inject an
EntityManager(or, better, an@EJB) inside a@FacesConverterwas an oversight in JSF spec and is scheduled to be fixed for upcoming JSF 2.2 which should be released by end of Q1 2012.The workaround is to make the converter a
@ManagedBeanor a@Namedbean instead, or to manually grab the EJB by JNDI.No, absolutely don’t do that. You’re mingling view with the model this way. Use a fullworthy
Converter.See also: