Entity with nested builder:
@Entity
@Table(name="food")
public class Food {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@Column(name="name")
private String name;
@Column(name="type")
private String type;
@Column(name="description")
private String description;
@Column(name="date")
private LocalDate expiration;
@ManyToOne
@JoinColumn(name="container_id", foreignKey = @ForeignKey(name = "FK_FOOD"))
private Container container;
private Food(FoodBuilder foodbuilder) {
this.name = foodbuilder.name;
this.type = foodbuilder.type;
this.description = foodbuilder.description;
this.expiration = foodbuilder.expiration;
}
//getters omitted for brevity
public static class FoodBuilder {
private String name;
private String type;
private String description;
private LocalDate expiration;
public FoodBuilder(String name) {
this.name = name;
}
public FoodBuilder setType(String type) {
this.type = type;
return this;
}
public FoodBuilder setDescription(String description) {
this.description = description;
return this;
}
public FoodBuilder setExpiration(LocalDate expiration) {
this.expiration = expiration;
return this;
}
public Food buildFood(){
return new Food(this);
}
}
}
I know how to use the main method to create a new object with the builder pattern i.e.
Food food = new Food.FoodBuilder...setters...build()
but I have been unable to find how I can use this pattern to create an object when I submit info via a form on the front-end to my api.