Bom dia pessoal, estou tentando fazer um converter aqui e estou quebrando a cabeça para resolver, o que acontece é que não grava o código do objeto (NULL), pesquisei, tentei de formas diferentes porem não obtive sucesso, caso alguém poder ajudar eu agradeço.
PS : Estou implementando hashCode e o equals
xhtml
<p:selectOneMenu id="sltEmpresas"
value="#{ClienteBean.cliente.empresa}" converter="EmpresaConverter">
<f:selectItem itemLabel="Selecione" itemValue="" />
<f:selectItems value="#{ClienteBean.empresas}" var="empresa"
itemLabel="#{empresa.nomeFantasiaEmpresa}" itemValue="#{empresa}"/>
</p:selectOneMenu>
converter
@FacesConverter(value = "EmpresaConverter",forClass = Empresa.class)
public class EmpresaConverter implements Converter {
@Override
public Object getAsObject(FacesContext fc, UIComponent component, String value) {
if (value != null || !"".equals(value)){
return this.getAttributesFrom(component).get(value);
}
return null;
}
@Override
public String getAsString(FacesContext fc, UIComponent component, Object value) {
if (value != null && !"".equals(value)){
Empresa empresa = (Empresa) value;
this.addAttribute(component, empresa);
Integer codigo = empresa.getCodigoEmpresa();
if (codigo != null){
return String.valueOf(codigo);
}
}
return (String) value;
}
protected void addAttribute(UIComponent component, Empresa e) {
String key = String.valueOf(e.getCodigoEmpresa());
this.getAttributesFrom(component).put(key, e);
}
protected Map<String, Object> getAttributesFrom(UIComponent component) {
return component.getAttributes();
}
}