- Create EntityConverter class which implements javax.faces.convert.Converter interface
- Override getAsObject() and getAsString() methods
How this converter works:
- If you need to convert entity to String, converter will create it from class canonical name and from value of id field. This field is annotated by @Id in your entity class.
- In case of other direction (convert to Object), it will split created string to class name and id value and try to load object from persistence storage.
Here is example of this converter:
import java.lang.reflect.Field; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.inject.Inject; import javax.inject.Named; import javax.persistence.EntityManager; import javax.persistence.Id; @Named public class EntityConverter implements Converter { @Inject private EntityManager em; public Object getAsObject(FacesContext fc, UIComponent component, String string) { try { String[] split = string.split(":"); return em.find(Class.forName(split[0]), Long.valueOf(split[1])); } catch (NumberFormatException | ClassNotFoundException e) { return null; } } public String getAsString(FacesContext fc, UIComponent component, Object object) { try { Class<? extends Object> clazz = object.getClass(); for (Field f : clazz.getDeclaredFields()) { if (f.isAnnotationPresent(Id.class)) { f.setAccessible(true); Long id = (Long) f.get(object); return clazz.getCanonicalName() + ":" + id.toString(); } } } catch (IllegalArgumentException | IllegalAccessException e) { } return null; } }And how to uset it? Not so big issue. In this example we want to show all user roles and user can choose from them:
<h:selectmanycheckbox converter="#{entityConverter}" value="#{bean.roles}"> <f:selectitems itemlabel="#{role.rolename]}" itemvalue="#{role}" value="#{bean.allRoles}" var="role"> </f:selectitems> </h:selectmanycheckbox>If you have different Id types in different classes you can use converter like this. It will call valueOf method on your Id field type class. For example you have field userId which is Integer type, it will call Integer.valueOf(valueFromPage). This way you can load entity from entityManager because you have type of your entity and id of this entity with correct type.
@Named public class EntityConverter implements Converter { @Inject private EntityManager em; @Inject Logger log; public Object getAsObject(FacesContext fc, UIComponent component, String string) { try { String[] split = string.split(":"); Class clazz = Class.forName(split[0]); for (Field f : clazz.getDeclaredFields()) { if (f.isAnnotationPresent(Id.class)) { Method valueOfMethod = f.getType().getMethod("valueOf", String.class); return em.find(clazz, valueOfMethod.invoke(null, split[1])); } } } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { log.warn("Cannot convert", e); } return null; } public String getAsString(FacesContext fc, UIComponent component, Object object) { try { Class clazz = object.getClass(); for (Field f : clazz.getDeclaredFields()) { if (f.isAnnotationPresent(Id.class)) { f.setAccessible(true); return clazz.getCanonicalName() + ":" + f.get(object); } } } catch (IllegalArgumentException | IllegalAccessException e) { log.warn("Cannot convert", e); } return null; }
very nice :)
ReplyDeletevery nice example
ReplyDeleteThis so complicated JSF Generic Converter.
ReplyDeleteHere is fully working JSF Generic Converter without need of EntityManager
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.WeakHashMap;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter(value = "entityConverter")
public class EntityConverter implements Converter {
private static Map entities = new WeakHashMap();
@Override
public String getAsString(FacesContext context, UIComponent component, Object entity) {
synchronized (entities) {
if (!entities.containsKey(entity)) {
String uuid = UUID.randomUUID().toString();
entities.put(entity, uuid);
return uuid;
} else {
return entities.get(entity);
}
}
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String uuid) {
for (Entry entry : entities.entrySet()) {
if (entry.getValue().equals(uuid)) {
return entry.getKey();
}
}
return null;
}
}
What happens if an object in the WeakHashMap gets GC'd?
DeleteNice one. One thing worth noticing is that Entity that's being converted this way need to override equals().
ReplyDeleteThank you!!
ReplyDelete