Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "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"
}

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading