0
I have a method in the application where I add the service date the warranty time the customer has. To execute the method I return from the database a list with all sales/services performed and when I run the method I have a new list already with the correct dates.
And since I already had a problem with Calendar that updates the dates kind of meaningless I return another database list with all sales/services and relate it to my list with the warranty dates.
However, when I return my list with the sales/services of the database the dates are the same as on my list with the guarantees.
How is this possible? Is it because of my entity that is the same? However, should not occur something like pqe am doing a new select.
My methods:
My Repository
public interface IVendaRepository extends JpaRepository<Venda, Long>{
List<Venda> findAll();
@Query(nativeQuery = true, value="Select * from jcf_tbl_cad_vendas")
List<Venda> listaVendas();
}
public List<Venda> EncontrarDatasVencimento(List<Venda> vendas){
List<Venda> lista = new ArrayList<>();
for(int i = 0; i< vendas.size(); i++) {
Venda v = new Venda();
v.setId(vendas.get(i).getId());
v.setData(vendas.get(i).getData());
v.setPrazo(vendas.get(i).getPrazo());
v.setVencimento(ObterVencimento(vendas.get(i).getData(),vendas.get(i).getTempo(),vendas.get(i).getPrazo()));
switch (vendas.get(i).getTempo()) {
case 0:
v.setDescPrazo("Dia(s)");
break;
case 1:
v.setDescPrazo("Semana(s)");
break;
case 2:
v.setDescPrazo("Mês(es)");
break;
case 3:
v.setDescPrazo("Ano(s)");
break;
default:
break;
}
lista.add(v);
}
return lista;
}
public List<Venda> ListaVendas(List<Venda> vendas, List<Venda> datas, List<ItemVenda> carrinho){
List<Venda> lista = new ArrayList<>();
for(int i = 0; i< vendas.size(); i++) {
Venda v = new Venda();
if(vendas.get(i).getId() == datas.get(i).getId()) {
v.setId(vendas.get(i).getId());
v.setVeiculo(vendas.get(i).getVeiculo());
v.setCliente(vendas.get(i).getCliente());
v.setUsuario(vendas.get(i).getUsuario());
v.setServico(vendas.get(i).isServico());
v.setData(vendas.get(i).getData());
v.setOrcamento(vendas.get(i).isOrcamento());
v.setSituacao(vendas.get(i).isSituacao());
v.setPrazo(vendas.get(i).getPrazo());
v.setDescPrazo(datas.get(i).getDescPrazo());
v.setVencimento(datas.get(i).getVencimento());
v.setTotal(ObterValorTotalVenda(vendas.get(i).getId(),carrinho));
lista.add(v);
}
}
return lista;
}
Where do I get the expiration dates of the service guarantee:
public Calendar ObterVencimento(Calendar data,int tempo ,int prazo) {
switch (tempo) {
case 0:
data.add(Calendar.DAY_OF_WEEK,prazo);
break;
case 1:
data.add(Calendar.WEEK_OF_MONTH,prazo);
break;
case 2:
data.add(Calendar.MONTH,prazo);
break;
case 3:
data.add(Calendar.YEAR,prazo);
break;
default:
break;
}
return data;
}
Main method to return the list I present to the user:
public List<Venda> findAll(){
List<Venda> datas = EncontrarDatasVencimento(vendaRepository.listaVendas());
List<Venda> vendas = vendaRepository.findAll();
return ListaVendas(vendas,datas,itemVendaRepository.findAll());
}
In the above method I run two different methods to return my sales, the result is the same. I ran different methods thinking that the problem might be that something related to using the same query (I still think it might be).
Result for the user:
Correct date that should appear in the Date Service column:
I don’t know why a change is affecting a select I’m making. Would anyone know why?
Oops, thank you so much for the change by adding the lime.clone() worked. But, know me know pqe this modification of the Calendar is so deep to affect the value of a new query in the database?
– DSCM
@DSCM O
Calendar
in itself has nothing to do with the database. What may be happening is that this modified date was being recorded in the bank, so the next query brought the new value. It’s the only way this happens, because theCalendar
alone cannot update the bank– hkotsubo
Yeah, I’m not saying that my date at the bank was changed is the result of my query that had the date changed. For this reason I was not understanding anything I was making a new query and my date was with the result of the modification I had made in obtaining my due date.
– DSCM