O erro acontece porque a instância da classe é a mesma.
Por isso, só é salvo uma vez!
Neste caso, você deve criar uma nova instância da classe DataFaltaSubstituicao para cada iteração do comando for.
public class TestResource {
private final SaleRepository saleRepository;
public TestResource(SaleRepository saleRepository) {
this.saleRepository = saleRepository;
}
@GetMapping
public ResponseEntity<SaleDto> test() {
Costumer thiago = Costumer.of("Thiago", LocalDate.of(1986, Month.NOVEMBER, 7), GenderEnum.MALE);
Product bola = Product.of("Bola", BigDecimal.valueOf(50));
Product televisao = Product.of("Televisão", BigDecimal.valueOf(2000));
Product mesaDeJantar = Product.of("Mesa de Jantar", BigDecimal.valueOf(3000));
Product laptop = Product.of("Laptop", BigDecimal.valueOf(7000));
Sale sale = Sale.of(thiago, Arrays.asList(bola, televisao, mesaDeJantar, laptop));
for (int i = 0; i < 10; i++) {
saleRepository.save(sale);
}
return ResponseEntity.ok(SaleDto.parse(sale));
}}
O resultado do código acima é:
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: insert into costumer (birth_date, gender, name, id) values (?, ?, ?, ?)
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: insert into product (name, price, sale_id, id) values (?, ?, ?, ?)
Hibernate: insert into product (name, price, sale_id, id) values (?, ?, ?, ?)
Hibernate: insert into product (name, price, sale_id, id) values (?, ?, ?, ?)
Hibernate: insert into product (name, price, sale_id, id) values (?, ?, ?, ?)
Analisando o log do hibernate
Veja que por mais que o comando for itere 10x não há atualização, ou seja, não houve update como foi sugerido em outra resposta.
Possível Solução
public class TestResource {
private final SaleRepository saleRepository;
public TestResource(SaleRepository saleRepository) {
this.saleRepository = saleRepository;
}
@GetMapping
public ResponseEntity<List<SaleDto>> test() {
List<SaleDto> saved = new ArrayList<>();
Costumer thiago = Costumer.of("Thiago", LocalDate.of(1986, Month.NOVEMBER, 7), GenderEnum.MALE);
Product bola = Product.of("Bola", BigDecimal.valueOf(50));
Product televisao = Product.of("Televisão", BigDecimal.valueOf(2000));
Product mesaDeJantar = Product.of("Mesa de Jantar", BigDecimal.valueOf(3000));
Product laptop = Product.of("Laptop", BigDecimal.valueOf(7000));
for (int i = 0; i < 10; i++) {
Sale savedSale = saleRepository.save(Sale.of(thiago, Arrays.asList(bola, televisao, mesaDeJantar, laptop)));
saved.add(SaleDto.parse(savedSale));
}
return ResponseEntity.ok(saved);
}}
Resultado do código acima:
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: insert into costumer (birth_date, gender, name, id) values (?, ?, ?, ?)
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: insert into product (name, price, sale_id, id) values (?, ?, ?, ?)
Hibernate: insert into product (name, price, sale_id, id) values (?, ?, ?, ?)
Hibernate: insert into product (name, price, sale_id, id) values (?, ?, ?, ?)
Hibernate: insert into product (name, price, sale_id, id) values (?, ?, ?, ?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: call next value for hibernate_sequence
Hibernate: insert into sale (costumer_id, date_of_sale, id) values (?, ?, ?)
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Hibernate: update product set name=?, price=?, sale_id=? where id=?
Analisando o log
Veja que agora houveram dez inserts de Vendas, um insert Cliente e quarenta updates de Produtos.
Finalizando
Ainda assim, é uma implementação ruim, esses 40 updates de produtos acontece porque o produto é o mesmo, e não deveria ser implementado dessa maneira. Apenas demostrei como seu código estava se comportando.
Documentação EntityManager