3

I have three classes:

Product:

@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;
}

Employee:

@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;
}
}

I have a controller class where i have this 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);
    }

For setEmployee i need to set a long parameters which is foreign key of employee_id but when I send request I need to send object. Also the same situation is with product, when I want to send request I need to send Object for Product but I want to send a foreign key parameter of product.

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

For example , quantity will be writed with the POST request but employee_id and product_id wont. Here is the table from the database: enter image description here

1 Answer 1

1

Assuming that employees and products already exists in DB, you should load every related entity first, in order to get an attached entity from JPA for each one. Then set them to the main entity.

The right code would look like this:

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.