springboot:mysql在尝试将客户Map到项目时出现重复键错误

h79rfbju  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(258)

我正在尝试构建一个带有一些基本电子商务功能(即添加到购物车等)的小型springbootcrud应用程序。我的基本实体是客户、奶酪和角色。
在尝试将客户Map到奶酪时,它只需要一个用户就可以很好地工作。生成一个“customer cheeses”表,其中包含customer accountnumber(id)和cheese id。下面是该表的图片:

但是,当尝试使用其他帐户添加项目时,会出现如下错误:
com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception:键“uk\u pg95jxw3noahgyna6qwbl3ivd”的重复条目“6”
我假设这仅仅意味着我只能有一个cheese id。我尝试过使用不同的hibernate注解(例如@elementcollection、@manytomy等),但都没能实现。
如有任何意见,我们将不胜感激。另外,请让我知道,如果你需要我的任何服务或控制器,但实际添加和删除是在我的应用程序正常工作。
顾客

package com.example.demo.models;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Customer implements Serializable {

    @NotNull
    @Size(min = 2, max = 25)
    private String name;

    @GeneratedValue
    @Id
    private int accountNumber;

    private BigDecimal accountFunds;

    @NotNull
    @Size(min = 2)
    private String password;

    @NotNull
    @Size(min = 2, max = 25)
    @Email
    private String email;

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="user_roles",
            joinColumns={@JoinColumn(name="CUSTOMER_EMAIL", referencedColumnName = "email")},
            inverseJoinColumns={@JoinColumn(name="ROLE_ID", referencedColumnName="id")})
    private List<Role> roles;

    @ElementCollection
    private List<Cheese> cheeses = new ArrayList<>();

    public Customer(String name, String password, String email) {
        this.name = name;
        this.password = password;
        this.email = email;
        this.accountFunds = new BigDecimal(225.00); // default value
    }

    public Customer() {}

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }

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

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAccountNumber() {
        return accountNumber;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public BigDecimal getAccountFunds() {
        return accountFunds;
    }

    public void setAccountFunds(BigDecimal accountFunds) {
        this.accountFunds = accountFunds;
    }

    public List<Cheese> getCheeses() {
        return cheeses;
    }

    public void setCheeses(List<Cheese> cheeses) {
        this.cheeses = cheeses;
    }
}

奶酪

package com.example.demo.models;

import javax.persistence.*;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Cheese {

    @NotNull
    @Size(min=2, max=20)
    private String name;

    @NotNull
    @Size(min=2, max=20)
    private String description;

    @NotNull
    @DecimalMax("10000.0") @DecimalMin("0.0")
    private BigDecimal price;

    @Id
    @GeneratedValue
    private int id;

    @ManyToMany
    private List<Customer> customers = new ArrayList<>();

    public Cheese() {}

    public Cheese(String name, String description, BigDecimal price) {
        this.name = name;
        this.description = description;
        this.price = price;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}
ryevplcw

ryevplcw1#

嗯…所以,我做了以下工作:我把email改成了customer\ id,这当然没什么区别。在奶酪课上,我做了“mappedby”奶酪。
再说一次,我不知道为什么这样做,另一个没有,但似乎是这样的。
顾客

package com.example.demo.models;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Customer implements Serializable {

@NotNull
@Size(min = 2, max = 25)
private String name;

@GeneratedValue
@Id
private int accountNumber;

private BigDecimal accountFunds;

@NotNull
@Size(min = 2)
private String password;

@NotNull
@Size(min = 2, max = 25)
@Email
private String email;

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="user_roles",
        joinColumns={@JoinColumn(name="CUSTOMER_EMAIL", referencedColumnName = "email")},
        inverseJoinColumns={@JoinColumn(name="ROLE_ID", referencedColumnName="id")})
private List<Role> roles;

//@ElementCollection

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="cheese_customers",
        joinColumns={@JoinColumn(name="CUSTOMER_ID", referencedColumnName = "accountNumber")},
        inverseJoinColumns={@JoinColumn(name="PRODUCT_ID", referencedColumnName="id")})
private List<Cheese> cheeses = new ArrayList<>();

public Customer(String name, String password, String email) {
    this.name = name;
    this.password = password;
    this.email = email;
    this.accountFunds = new BigDecimal(225.00);
}

public Customer() {}

public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getEmail() {
    return email;
}

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

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAccountNumber() {
    return accountNumber;
}

public List<Role> getRoles() {
    return roles;
}

public void setRoles(List<Role> roles) {
    this.roles = roles;
}

public BigDecimal getAccountFunds() {
    return accountFunds;
}

public void setAccountFunds(BigDecimal accountFunds) {
    this.accountFunds = accountFunds;
}

public List<Cheese> getCheeses() {
    return cheeses;
}

public void setCheeses(List<Cheese> cheeses) {
    this.cheeses = cheeses;
}
}

奶酪

package com.example.demo.models;

import javax.persistence.*;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Cheese {

    @NotNull
    @Size(min=2, max=20)
    private String name;

    @NotNull
    @Size(min=2, max=20)
    private String description;

    @NotNull
    @DecimalMax("10000.0") @DecimalMin("0.0")
    private BigDecimal price;

    @Id
    @GeneratedValue
    private int id;

    @ManyToMany(mappedBy = "cheeses")
    private List<Customer> customers = new ArrayList<>();

    public Cheese() {}

    public Cheese(String name, String description, BigDecimal price) {
        this.name = name;
        this.description = description;
        this.price = price;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

相关问题