简体   繁体   English

使用 spring boot Jpa 插入 mysql 不工作

[英]Insert Into mysql using spring boot Jpa not working

I am trying to persist data into a Mysql db using JpaRepository in spring boot however I keep getting the following error, not sure what am doing wrong:我试图在 Spring Boot 中使用 JpaRepository 将数据保存到 Mysql 数据库中,但是我不断收到以下错误,不知道做错了什么:

 java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails

My Model classes looks like:我的模型类看起来像:

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

    @Id
    @Column(name = "id", unique = true, nullable = false)
    private String id;
    @Column(name = "user_id")
    private int userId;

    @OneToMany(fetch = FetchType.LAZY, targetEntity = User.class)
    @JoinColumn(name="user_id", referencedColumnName = "id", insertable = false, updatable = false)
    private List<User> user;

    public List<User> getUser() {
        return user;
    }

    public void setUser(List<User> user) {
        this.user = user;
    }

    public Cart() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }
}


@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false, columnDefinition = "integer")
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "email")
    private String email;
    @Column(name = "phone")
    private String phone;

    public User() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

My repository:我的存储库:

    @Repository
public interface CartRepository extends JpaRepository<Cart, String> {


}

What am i doing wrong and is there a better way to persist data?我做错了什么,有没有更好的方法来保存数据?

Update: This is the data am using on postman:更新:这是邮递员使用的数据:

{
    "id": "5beeb524-20fc-11ea-978f-2e728ce88125",
    "user_id": 1
}

However when i log the above data, it returns the id as above but the user_id always returns 0但是,当我记录上述数据时,它返回上述 id 但 user_id 始终返回 0

The id for cart is not auto-generated.购物车的 ID 不是自动生成的。

Add a generator for the unique id.为唯一 id 添加一个生成器。 For example, for a string the generator could be like :例如,对于一个字符串,生成器可能是这样的:

@Id
@GeneratedValue(generator = "uuid")
@Column(name = "id", unique = true, nullable = false)
private String id;

Find a suitable type and generator for your unique id accordingly.相应地为您的唯一 ID 找到合适的类型和生成器。 I hope it helps.我希望它有帮助。

EDIT: After seeing the stack trace, this also look somewhat helpful : could not execute statement SQL constraint [id] nested exception is org.hibernate.exception.ConstraintViolationException编辑:在看到堆栈跟踪后,这看起来也有些帮助: 无法执行语句 SQL 约束 [id] 嵌套异常是 org.hibernate.exception.ConstraintViolationException

Also the issue seems to because the cart.user_id is a foreign key referencing the user.id in user class which is violating some constraints.此外,问题似乎是因为 cart.user_id 是引用 user.id 的外键,它违反了一些约束。

Add the following to user model class :将以下内容添加到用户模型类:

@OneToMany(mappedBy = "user_id", cascade = CascadeType.ALL)
private List<User> users;

And add the following to cart model class :并将以下内容添加到购物车模型类:

@ManyToOne
@JoinColumn
private int user_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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