我想在我的jsp上显示类似这样的内容
CategoryParent1
-----子类别1
----- Subcategory2
CategoryParent2
-----子类别1
----- Subcategory2
我能以哪种方式解决它? spring-mvc有一些库吗?或者我必须使用一些外部库?
我有一个类别模型
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
@ManyToOne
private Category parentCategory;
@OneToMany(mappedBy = "parentCategory", cascade = CascadeType.ALL)
private List<Category> childCategories = new ArrayList<>();
public Category() {
}
public Category(String name){
this.name = name;
}
public Category(String name, Category parentCategory) {
this.name = name;
this.parentCategory = parentCategory;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getParentCategory() {
return parentCategory;
}
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}
public List<Category> getChildCategories() {
return childCategories;
}
public void setChildCategories(List<Category> childCategories) {
this.childCategories = childCategories;
}
}
答案 0 :(得分:1)
我已经完成了类似的任务但是使用了递归查询。 Here is the link
如果您不想使用递归查询。这是你需要做的。
不要使用List更好地使用Set:
@OneToMany(fetch = FetchType.EAGER, mappedBy="parentCategory", cascade=CascadeType.REMOVE, orphanRemoval=true )
private Set<Categories> childCategories = new HashSet<Categories>();