简体   繁体   中英

Java Spring Boot: ERROR: null value in column "user_id" violates not-null constraint Detail: Failing row contains (36, null, null, 0)

I keep getting this Error: null value in column "user_id" violates not-null constraint Detail: Failing row contains (36, null, null, 0). I have setUser in the Cart Object (The user exists in the database). But when i try to save it the value remains null for the User Object: (36, null , null, 0).

Cartcontroller.class

@PostMapping(value = "/api/carts/users/{userId}/products/{productId}")
    public Cart addProductInCart(@PathVariable int userId, @PathVariable int productId) {
            Cart cart = new Cart();
            cart.setUser(userRepository.findById(userId));
            cart.setProduct(productRepository.findById(productId));

            return cartRepository.save(cart);
        }

Cart.class

@Entity
@Table(name = "cart")
public class Cart {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
    private User user;


    @ManyToOne
    @JoinColumn(name = "product_id", nullable = false, insertable = false, updatable = false)
    private Product product;

    @Column(name = "amount")
    private int amount;

^contains getters and setters

User.class

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "username")
    public String username;

    @Column(name = "password")
    public String password;

    @Column(name = "role")
    public String role;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
    private Set<Cart> carts;

^contains getters and setters

try

@PostMapping(value = "/api/carts/users/{userId}/products/{productId}")
public Cart addProductInCart(@PathVariable int userId, @PathVariable int productId) {
  Cart cart = new Cart();
  User user = userRepository.findById(userId);
  user.getCarts().add(cart);
  cart.setUser(user);
  cart.setProduct(productRepository.findById(productId));

  return cartRepository.save(cart);
}

alternatively I would recomment a method:

public void addCart(Cart cart) {
  if(cart != null) {
    this.carts.add(cart);
    cart.setUser(this);
  }
}

in your User class. Also make sure that your carts variable is initialized, I usually do that on declaration, like this:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private Set<Cart> carts = new HashSet<>();

or you just add it to the constructor or getter. You migh also have to add similar functionality for Product , if you have a bidirectional relationship you need to maintain it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM