Olá, boa tarde.
Estou tendo o seguinte erro na hora de executar o código:
Row was already updated or deleted by another transaction for entity [br.com.pedropgaraujo.purchasemanager.model.Category with id '3']
Irei deixar abaixo minha classe category e a principal onde está sendo executado o código.
Category.class
package br.com.pedropgaraujo.purchasemanager.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
private List<Product> productList = new ArrayList<>();
public Category(){}
public Category(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public List<Product> getProductList() {
return productList;
}
public void setProductList(List<Product> productList) {
this.productList = productList;
}
}
Principal.class
package br.com.pedropgaraujo.purchasemanager;
import java.time.LocalDate;
import java.util.List;
@Component
public class Principal {
@Autowired
private ProductRepository productRepository;
@Autowired
private CategoryRepository categoryRepository;
@Autowired
private ClientOrderRepository clientOrderRepository;
@Autowired
private SupplierRepository supplierRepository;
public Principal (ProductRepository productRepository, CategoryRepository categoryRepository, ClientOrderRepository clientOrderRepository){
this.categoryRepository = categoryRepository;
this.productRepository = productRepository;
this.clientOrderRepository = clientOrderRepository;
}
Category categoryFurniture = new Category(1L, "Furniture");
Category categoryBooks = new Category(2L, "Books");
Category categoryEletronics = new Category(3L, "Eletronics");
Product product1 = new Product("Notebook",5000.0, categoryEletronics);
Product product2 = new Product("Smartphone", 2500.0, categoryEletronics);
Product product3 = new Product("Livro Java", 150.0, categoryBooks);
Product product4 = new Product("Livro Spring Boot", 100.0, categoryBooks);
Supplier supplierTech = new Supplier("Tech Supplier");
Supplier supplierBook = new Supplier("Global Library");
ClientOrder clientOrder1 = new ClientOrder(1L, LocalDate.now());
ClientOrder clientOrder2 = new ClientOrder(2L, LocalDate.now().minusDays(1));
public void principalTwo(){
//Creating category on DB
categoryRepository.saveAll(List.of(categoryEletronics,categoryBooks,categoryFurniture));
//Creating suppliers on DB
supplierRepository.saveAll(List.of(supplierTech, supplierBook));
//Creating products on DB
product1.setSupplier(supplierTech);
product2.setSupplier(supplierTech);
product3.setSupplier(supplierBook);
product4.setSupplier(supplierBook);
productRepository.saveAll(List.of(product1, product2, product3, product4));
//Creating Orders
clientOrder1.setProductList(List.of(product1, product3));
clientOrder2.setProductList(List.of(product2, product4));
//Testing consults and verify relationship
System.out.println("Products on category Eletronics: ");
categoryRepository.findById(1L).ifPresent(c ->
c.getProductList().forEach(p ->
System.out.println(" - " + p.getName())
)
);
System.out.println("\nOrder and products: ");
clientOrderRepository.findAll().forEach(o -> {
System.out.println("Order " + o.getId() + ":");
o.getProductList().forEach(p ->
System.out.println(" - " + p.getName()));
});
System.out.println("\nProducts and suppliers: ");
productRepository.findAll()
.forEach(p ->
System.out.println(
"Product: " + p.getName() +
", Supplier: " + p.getSupplier().getName()));
}
}
Retirei alguns imports deste código postado aqui, somente porque atingiu o limite máximo de caracteres.