Java 这些Spring/Hibernate实体是否适合烹饪应用程序(性能方面)?
我想在数据库中存储食谱和配料。我提出了这些实体:Java 这些Spring/Hibernate实体是否适合烹饪应用程序(性能方面)?,java,spring,hibernate,Java,Spring,Hibernate,我想在数据库中存储食谱和配料。我提出了这些实体: @Entity public class Recipe { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String notes; @OneToMany(mappedBy = "recipe", cascade = Casca
@Entity
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String notes;
@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL)
private List<IngredientQuantity> ingredientQuantities = new ArrayList<>();
}
现在,当我想要创建一个新的配方时,我必须检查每个成分是否已经存在于db中。这种方法好吗?如果可以的话,我是用“WHERE(…)IN()”子句打一个电话给db提取所有成分,还是用多个查询点击db?不要问基于意见的问题-没有好的或坏的实体,如果它有效,你将通过实践和阅读书籍来学习编写更好的代码。另外,不要过早地担心性能问题(过早优化实际上是反模式的),只有在以后(如果有的话)才能解决性能问题needed@JAsgarov好的,谢谢!
@Entity
public class Ingredient {
@Id
private String name;
@OneToMany(mappedBy = "ingredient", cascade = CascadeType.ALL)
private List<IngredientQuantity> ingredientQuantities = new ArrayList<>();
}
@Entity
public class IngredientQuantity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "fk_recipe")
private Recipe recipe;
@ManyToOne
@JoinColumn(name = "fk_ingredient")
private Ingredient ingredient;
private double quantity;
}