I need to create a treatment for, if no investment fund registration is found, avoid error and nullpointer.
You are returning the error:
java.lang.IllegalArgumentException: id to load is required for loading
Line to be treated:
fu.setFundo(fundoService.findByCoFundoAnbima(fu.getCoFundoAnbima()).getId());
Method:
if (fundos != null) {
for (FundoSimulacao fu : fundos) {
fu.setSimulacao(simulacao);
fu.setFundo(fundoService.findByCoFundoAnbima(fu.getCoFundoAnbima()).getId());
fundoSimulacaoService.save(fu);
}
}
FundoService.java
/**
* Carrega o fundo conforme o id
*
* @param id
* @return Fundo
*/
public Fundo findByCoFundoAnbima(Integer id) {
return fundoRepository.findById(id);
}
FundoRepository.java
@Repository
public class FundoRepository extends AbstractRepository<Fundo> {
private static final Logger LOG = Logger.getLogger(FundoRepository.class);
/**
*
*/
public FundoRepository() {
super(Fundo.class);
}
/**
* retorna a lista de simulações
*
* @return List<Fundo>
*/
public List<Fundo> get() {
LOG.info(" List<Fundo> get() ");
TypedQuery<Fundo> tquery = getEntityManager().createNamedQuery(Constants.FUNDO_NAMED_QUERY, Fundo.class);
return tquery.getResultList();
}
}
Fundo.java Entity
@Entity
@Table(schema = Constants.SIMFI_SCHEMA_NAME, name = Constants.FUNDO_TABLE_NAME)
@NamedQueries({ @NamedQuery(name = Constants.FUNDOS_NAMED_QUERY, query = Constants.FUNDOS_QUERY) })
public class Fundo implements AbstractEntity<Integer> {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = Constants.CO_FUNDO)
private Integer id;
@Column(name = Constants.CO_FUNDO_ANBIMA, nullable = false)
private Integer fundoAnbima;
@Column(name = Constants.DE_OBJETIVO)
private String objetivo;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = Constants.CO_ESTRATEGIA)
private Estrategia estrategia;
@Override
public Integer getId() {
return id;
}
@Override
public void setId(Integer id) {
this.id = id;
}
public void setEstrategia(Estrategia estrategia) {
this.estrategia = estrategia;
}
public Estrategia getEstrategia() {
return estrategia;
}
public void setFundoAnbima(Integer fundoAnbima) {
this.fundoAnbima = fundoAnbima;
}
public Integer getFundoAnbima() {
return fundoAnbima;
}
public void setObjetivo(String objetivo) {
this.objetivo = objetivo;
}
public String getObjetivo() {
return objetivo;
}
}
Entidade FundoSimulacao.java
@Entity
@Table(schema = Constants.SIMFI_SCHEMA_NAME, name = Constants.FUNDO_SIMULACAO_TABLE_NAME)
@NamedQueries({ @NamedQuery(name = Constants.FUNDO_SIMULACAO_NAMED_QUERY, query = Constants.FUNDO_SIMULACAO_QUERY) })
public class FundoSimulacao implements AbstractEntity<Integer> {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = Constants.CO_FUNDO_SIMULACAO)
private Integer id;
@Column(name = Constants.CO_FUNDO, nullable = false)
private Integer fundo;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = Constants.CO_SIMULACAO)
private Simulacao simulacao;
@Transient
private Integer coFundoAnbima;
@Override
public Integer getId() {
return id;
}
@Override
public void setId(Integer id) {
this.id = id;
}
public Integer getFundo() {
return fundo;
}
public void setFundo(Integer fundo) {
this.fundo = fundo;
}
public Simulacao getSimulacao() {
return simulacao;
}
public void setSimulacao(Simulacao simulacao) {
this.simulacao = simulacao;
}
public Integer getCoFundoAnbima() {
return coFundoAnbima;
}
public void setCoFundoAnbima(Integer coFundoAnbima) {
this.coFundoAnbima = coFundoAnbima;
}
}