2
I need help, I’m trying to create a fixed expense launch. I have to insert for example 12 records in the database, through a loop inside the for.
To enter the data I am using Hibernate, however it only inserts a line, the 11 lines he should insert it does not insert, already debugged and there are no errors
for (int i = 1; i <= duracaoRepeticao; i++) {
System.out.println("loop : " + i);
Calendar calendar = new GregorianCalendar(); // cria gregorian calendar
calendar.setTime(dataLancamento); // seta a data informada no formulario no calendar
calendar.add(Calendar.MONTH, i); // seta incrementalmente (mensalmente) o mes, mantendo a data statica
Date dataLancamentoIncremental = calendar.getTime(); lancamentoDados.setDataLancamentoDados(dataLancamentoIncremental);
lancamentoDados.setCategoria(lancamentoDados.getCategoria());
lancamentoDados.setValorPagoLancamentoDados(123);
lancamentoDadosDao.adiciona(lancamentoDados); // cadastra detalhes do lancamento, parcela, despesa fixa...
}
This is the output in the Tomcat log
Um por mes
loop : 1
data: Sat Jul 02 00:00:00 BRT 2016
Passei perto
Hibernate:
insert
into
LANCAMENTO_DADO
(LCD_COD_CAT_COD, LCD_COD_CTA_COD, LCD_DATAPARALANCAR, LCD_COD_PES_COD, LCD_COD_LCA_COD, LCD_STATUS, LCD_COD_SCT_COD, LCD_VALOR, LCD_VALORPAGO)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
Passe do session
loop : 2
data: Tue Aug 02 00:00:00 BRT 2016
Passei perto
Passe do session
loop : 3
data: Fri Sep 02 00:00:00 BRT 2016
Passei perto
Passe do session
loop : 4
data: Sun Oct 02 00:00:00 BRT 2016
Passei perto
Passe do session
loop : 5
data: Wed Nov 02 00:00:00 BRST 2016
Passei perto
Passe do session
loop : 6
data: Fri Dec 02 00:00:00 BRST 2016
Passei perto
Passe do session
loop : 7
data: Mon Jan 02 00:00:00 BRST 2017
Passei perto
Passe do session
loop : 8
data: Thu Feb 02 00:00:00 BRST 2017
Passei perto
Passe do session
loop : 9
data: Thu Mar 02 00:00:00 BRT 2017
Passei perto
Passe do session
loop : 10
data: Sun Apr 02 00:00:00 BRT 2017
Passei perto
Passe do session
loop : 11
data: Tue May 02 00:00:00 BRT 2017
Passei perto
Passe do session
loop : 12
data: Fri Jun 02 00:00:00 BRT 2017
Passei perto
Passe do session
Someone knows how to help me ?
This yes, I checked here. Tomcat viewer from loop two does not see Insert action like this above
– Ivan Alves
From what you can see, throughout the loop roll dice is a single object Voce should create a new object at each loop
for(int i=0;i<duracaoRepeticao;i++){
 LancamentoDados lancamentosDados = new LancamentoDados();
 //preenche o objeto
 lancamentosDadosDao.adiciona(lancamentosDados);

}
– Jeterson Miranda Gomes
It worked out here, Valew, problem solved
– Ivan Alves
Put your solution there, And if the answer was useful do not forget to mark it. Thank you
– Jeterson Miranda Gomes