packagetacos;importjava.util.Date;importjava.util.List;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.ManyToMany;importjavax.persistence.OneToMany;importjavax.persistence.PrePersist;importjavax.validation.constraints.NotNull;importjavax.validation.constraints.Size;importlombok.Data;@Data@EntitypublicclassTaco { @Id @GeneratedValue(strategy=GenerationType.AUTO)privateLong id; @NotNull @Size(min=5, message="Name must be at least 5 characters long")privateString name;privateDate createdAt; @ManyToMany(targetEntity=Ingredient.class) @Size(min=1, message="You must choose at least 1 ingredient")privateList<Ingredient> ingredients; @PrePersistvoidcreatedAt() {this.createdAt=newDate(); }}
与 Ingredient 一样,Taco 类现在使用 @Entity 注解,其 id 属性使用 @Id 注解。因为依赖于数据库自动生成 id 值,所以还使用 @GeneratedValue 注解 id 属性,指定自动策略。