Home > OS >  Error: Duplicate key value violates uniqueness constraint
Error: Duplicate key value violates uniqueness constraint

Time:07-10

When trying to send a request, with the same "flower_id", to Postman, returns 500 with message:

"could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement."

At the same time, it does not matter if the same ids are in the same request or in different ones with different users, if one flower has already been added earlier, it is no longer possible to add it to another user.

Entity Order:

import javax.persistence.*;
import java.time.LocalDate;
import java.util.List;

@Entity
@Table(name = "orders")
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private LocalDate orderCreateDate;
    private LocalDate orderCompleteDate;
    @ManyToOne(fetch = FetchType.LAZY)
    private User user;
    @ManyToMany
    private List<Flower> flower;
    private Integer price;

    public Order() {
    }

    public Order(LocalDate orderCreateDate, LocalDate orderCompleteDate, User user, List<Flower> flower) {
        this.orderCreateDate = orderCreateDate;
        this.orderCompleteDate = orderCompleteDate;
        this.user = user;
        this.flower = flower;
    }

    //Getters and setters
}

Entity Flower:

import javax.persistence.*;

@Entity
@Table(name = "flowers")
public class Flower {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private Integer price;

    public Flower() {
    }

    public Flower (String name, Integer price) {
        this.name = name;
        this.price = price;
    }

    //Getters and Setters
}

OrderService:

import com.learning.flowershop.Entity.Order;
import com.learning.flowershop.Repositories.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;

@Service
public class OrderService {
    private final OrderRepository orderRepository;

    @Autowired
    public OrderService(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    public List<Order> getAllOrdersByUserId(Long userId) {
        return orderRepository.findAllByUserId(userId);
    }

    @Transactional
    public void saveOrder(Order order) {
        orderRepository.save(order);
    }
}

CodePudding user response:

Did you check the constraints in your database? The 500 error indicates an internal server error. It seems like there might be a unique constraint in your relation table which causes an SQL exception. If this exception is not properly caught it will get rethrown as an internal server error.

CodePudding user response:

I still don't fully understand why this is the case, but I still want to leave a solution to my question.

It was only worth adding a save method for User

 public User saveUser(User user) {
    return userRepository.save(user);
}

Which is strange, because before that all my users were quietly saved.

  • Related