I'm implementing the Converter below:
@FacesConverter(forClass = Cargo.class)
public class CargoConverter implements Converter {
@Inject
private CargosRepository cargoRepository;
public CargoConverter() {
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Cargo retorno = null;
if (value != null) {
retorno = this.cargoRepository.porId(new Long(value));
}
return retorno;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null) {
Long id = ((Cargo) value).getId();
String retorno = (id == null ? null : id.toString());
return retorno;
}
return "";
}
}
At line Long id = ((Cargo value).getId()
is giving the exception above.
My model is implemented like this:
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
In the model hasCode also implemented.
How to resolve this exception? What am I doing wrong?
Exception:
java.lang.ClassCastException: java.lang.Long cannot be cast to com.porto.npf.sgpsweb.model.Cargo
at com.porto.npf.sgpsweb.converter.CargoConverter.getAsString(CargoConverter.java:31)