【发布时间】:2019-07-10 05:50:36
【问题描述】:
我有两个类 Food 和 FoodType:
public class Food {
//other attributes and constructor and getters and setters are ignored
@ManyToMany(targetEntity = FoodType.class, fetch = FetchType.LAZY)
@JoinTable(name = "food_type_",
joinColumns = {@JoinColumn(name = "fid")},
inverseJoinColumns = {@JoinColumn(name = "tid")})
private List<FoodType> types = new ArrayList<>();
}
public class FoodType {
@Id
@GeneratedValue
private long id;
private String type;
@ManyToMany(targetEntity = Food.class, fetch = FetchType.LAZY, mappedBy = "types")
private List<Food> foodList = new ArrayList<>();
//constructor and getters and setters are ignored
}
要求是:应该允许用户通过视图添加新的食物,所以我想知道如何在视图中将 FoodType 添加到食物?
我读过这个answer,看来在视图中操作数据不是一个好主意,所以如果你是程序员,你会如何设计 MVC 层以便它能够正确处理需求?
PS:我对JS知之甚少,可以在Thymeleaf中做吗?
【问题讨论】:
-
是的,在百里香中是可能的。我认为您的映射应该是
OneToOne,因为一种食物可能有一种类型。如果在您的情况下它可能有多种类型,那么OneToMany。你可以关注这个baeldung.com/thymeleaf-list
标签: spring-boot spring-mvc thymeleaf