如何在具有manyToOne关系的表中使用SpringBoot发布

时间:2018-03-16 09:34:15

标签: java postgresql hibernate spring-boot

我有三个班级:

产品:

@Entity
@Table(name ="product")
public class Product {

private long product_id;
private String name;
private double priceWithTax;
private String tax;
private String transportPackage;
private double l_kginTP;
private String category;
private Set<EmployeeProduct> employeeProducts=new HashSet<EmployeeProduct>();

public Product() {
    super();
}

@Id
@GeneratedValue
@Column(name = "product_id")
public long getProduct_id() {
    return product_id;
}

public void setProduct_id(long product_id) {
    this.product_id = product_id;
}

public String getName() {
    return name;
}

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

public double getPriceWithTax() {
    return priceWithTax;
}

public void setPriceWithTax(double priceWithTax) {
    this.priceWithTax = priceWithTax;
}

@OneToMany(mappedBy = "product")
public Set<EmployeeProduct> getEmployeeProducts() {
    return employeeProducts;
}

public void setEmployeeProducts(Set<EmployeeProduct> employeeProducts) {
    this.employeeProducts = employeeProducts;
}

public String getTax() {
    return tax;
}

public void setTax(String tax) {
    this.tax = tax;
}

public String getTransportPackage() {
    return transportPackage;
}

public void setTransportPackage(String transportPackage) {
    this.transportPackage = transportPackage;
}

public double getL_kginTP() {
    return l_kginTP;
}

public void setL_kginTP(double l_kginTP) {
    this.l_kginTP = l_kginTP;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

员工:

@Entity
@Table(name = "employee")
public class Employee {

private long employee_id;
private String name;
private String lastName;
private Set<EmployeeProduct> employeeProducts = new HashSet<EmployeeProduct>();

public Employee() {
    super();
}


public Employee(long id, String name, String lastName) {
    super();
    this.employee_id = id;
    this.name = name;
    this.lastName = lastName;
}

public void addEmployeeProduct(EmployeeProduct eproduct)
{
    this.employeeProducts.add(eproduct);
}

@Id
@GeneratedValue
@Column(name="employee_id")
public long getEmployee_id() {
    return employee_id;
}

public void setEmployee_id(long employee_id) {
    this.employee_id = employee_id;
}

public String getName() {
    return name;
}

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

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

@OneToMany(mappedBy="product")
public Set<EmployeeProduct> getEmployeeProducts() {
    return employeeProducts;
}

public void setEmployeeProducts(Set<EmployeeProduct> employeeProducts) {
    this.employeeProducts = employeeProducts;
}   
}

EmployeeProduct:

@Entity
@Table(name="EmployeeProduct")
public class EmployeeProduct {

private long employee_product_id;
private Employee employee;
private Product product;
private Date date;
private double quantityTaken;
private double quantityReturn;
private double quantitySale; 

@Id
@GeneratedValue
@Column(name = "employee_product_id")

public long getEmployee_product_id() {
    return employee_product_id;
}

public void setEmployee_product_id(long employee_product_id) {
    this.employee_product_id = employee_product_id;
}


@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "employee_id") 
public Employee getEmployee() {
    return employee;
}

public void setEmployee(Employee employee) {
    this.employee = employee;
}

@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "product_id")
public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}


@Column(name = "date")
public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public double getQuantitySale() {
    return quantitySale;
}

public void setQuantitySale(double quantitySale) {
    this.quantitySale = quantitySale;
}

public double getQuantityTaken() {
    return quantityTaken;
}

public void setQuantityTaken(double quantityTaken) {
    this.quantityTaken = quantityTaken;
}

public double getQuantityReturn() {
    return quantityReturn;
}

public void setQuantityReturn(double quantityReturn) {
    this.quantityReturn = quantityReturn;
}
}

我有一个控制器类,我有这个POST:

 @CrossOrigin
    @RequestMapping(value = "/distribution/create", method = RequestMethod.POST)
    public void addEmployee(@RequestBody EmployeeProduct e) {
        EmployeeProduct employeeProduct = new EmployeeProduct();
        employee.setEmployee(e.getEmployee());
        employee.setProduct(e.getProduct());
        employeeService.addEmployee(employeeProduct);
    }

对于setEmployee,我需要设置一个long参数,这是employee_id的外键,但是当我发送请求时我需要发送对象。同样的情况是产品,当我想发送请求我需要发送产品的对象,但我想发送产品的外键参数。

{
    "employee_id":1,
    "product_id":1,
    "quantity":5
}

例如,数量将与POST请求一起写入,但employee_id和product_id不会。 这是数据库中的表: enter image description here

1 个答案:

答案 0 :(得分:1)

假设员工和产品已存在于数据库中,您应首先加载每个相关实体,以便从JPA获取每个相关实体。然后将它们设置为主实体。

正确的代码如下所示:

EmployeeProduct employeeProduct = new EmployeeProduct();

Employee employee = employeeRepository.findOne(e.getEmployeeId());
employeeProduct.setEmployee(employee);

Product product= productRepository.findOne(e.getProductId());
employeeProduct.setProduct(product);

employeeService.addEmployee(employeeProduct);