Data Calendar updating alone

Asked

Viewed 28 times

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:

inserir a descrição da imagem aqui

Correct date that should appear in the Date Service column:

inserir a descrição da imagem aqui

I don’t know why a change is affecting a select I’m making. Would anyone know why?

2 answers

1


In the method ObterVencimento you call the method add, that modifies the Calendar. For example, if you do this:

Calendar cal = Calendar.getInstance(); // Calendar com a data atual
System.out.println(cal.getTime()); // mostra a data atual

ObterVencimento(cal, 2, 3);
System.out.println(cal.getTime()); // mostra a data 3 meses à frente

See that even without catching the return of the method, the Calendar was modified.

To avoid this, you could create a copy of Calendar and return this modified copy:

public Calendar ObterVencimento(Calendar cal, int tempo, int prazo) {
    // cria uma cópia
    Calendar data = (Calendar) cal.clone();

    // modifica a cópia
    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; // retorna a cópia modificada
}

I also suggest changing the name to obterVencimento (with the first lowercase letter), to be attached with the Java code conventions.

  • 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 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 the Calendar alone cannot update the bank

  • 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.

0

In java we do not have passage by reference, only copy, however, the copy points to the same references.

So when you do: data.add(Calendar.WEEK_OF_MONTH, prazo); is changing the original variable.

The suggestion of the colleague: @hkotsubo clone, it works, because the class Calendar implements the interface Cloneable thus, a new object is created with the same attributes (a copy totally)

The change in the database occurs because you searched from the database (the object is in the state Managed) and when you make a change to the object, the data is persisted in the database automatically.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.