Supplier entity using JPA annotations to create a one to may relation with items + cascading its persistence to the items so they can be saved as well
package com.project.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
@Table(name="supplier")
public class Supplier {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
private String phone;
@JsonManagedReference
@OneToMany(mappedBy="supplier", cascade={CascadeType.PERSIST, CascadeType.MERGE})
private List<Item> items = new ArrayList<Item>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
// @JsonIgnore // to not get all items when calling the suppliers only
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
items.forEach(item->item.setSupplier(this));
this.items = items;
}
public long getId() {
return id;
}
}