Home > OS >  How can i save the child entity while saving the parent entity to the database?
How can i save the child entity while saving the parent entity to the database?

Time:04-29

I'm trying to develop a mini e-commerce project. I have a Basket and BasketItem entity. I just want to when i saving a basket for the customer, I want the basket items to be saved in the database.I think I shouldn't create repository for Basket item.I should be able to save the basket item while saving the basket with the basket repository.

public class Basket extends BaseModel {

    @Column(nullable = false)
    private BigDecimal price;

    private BigDecimal discountPrice = BigDecimal.ZERO;
    private BigDecimal taxPrice = BigDecimal.ZERO;
    private BigDecimal shippingPrice = BigDecimal.ZERO;

    @Column(nullable = false)
    private BigDecimal totalPrice;

    @OneToMany(mappedBy = "basket", cascade = CascadeType.PERSIST)
    private Set<BasketItem> items = new HashSet<>();

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "customer_id", nullable = false)
    private Customer customer;

}
public class BasketItem extends BaseModel {

    @ManyToOne(optional = false)
    private Basket basket;

    @ManyToOne(optional = false)
    private Product product;

    @Column(nullable = false)
    private Long quantity;
    @Column(nullable = false)
    private BigDecimal price = BigDecimal.ZERO;
    private BigDecimal discountPrice = BigDecimal.ZERO;
    private BigDecimal taxPrice = BigDecimal.ZERO;
    private BigDecimal shippingPrice = BigDecimal.ZERO;
}

Here I am trying to get a basket item via basket service and set it to basket entity and save it.

public class BasketServiceImpl implements BasketService{

    private final CustomerRepository customerRepository;
    private final BasketRepository basketRepository;
    private final BasketItemConverter basketItemConverter;

    @Override
    public void addBasketItemToBasket(Long customerId, AddBasketItemDTO addBasketItemDTO) {
        //Find customer
        Customer customer = customerRepository.findById(customerId).orElseThrow(
                () -> new BusinessServiceOperationException.CustomerNotFoundException("Customer not found")
        );
        //Convert AddBasketItemDto to BasketItem
        BasketItem basketItem = basketItemConverter.toBasketItem(addBasketItemDTO);
        Set<BasketItem> basketItemsList = new HashSet<BasketItem>();
        Basket basket = new Basket();
        basketItemsList.add(basketItem);

        basket.setItems(basketItemsList);
        basket.setCustomer(customer);
        basket.setPrice(BigDecimal.ZERO);
        basket.setTotalPrice(BigDecimal.ZERO);
        basketRepository.save(basket);
    }
}

What is my problem? I got this exception.

{
    "errorMessage": "detached entity passed to persist: org.patikadev.orderexample.model.BasketItem; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: org.patikadev.orderexample.model.BasketItem"
}

CodePudding user response:

Yes you dont need a basket item repository if the Basket having proper persistent linking. But you will have to save Item first and then add that to basket and save the basket at the very end.

  • Related