2
I am 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 "";
}
}
On the 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;
The model is also implemented hasCode.
How to solve 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)
What exception? I guess you forgot to include it
– Bruno César
value
is already coming to you like aLong
, then you don’t have to do cast for an objectCargo
and then retrieve the id– Bruno César