I have two classes representing menu items.
First one is Category
, it represents the parent menu items.
@Entity
@Table(name = "category")
@NamedQueries({
@NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
@NamedQuery(name = "Category.findByCateId", query = "SELECT c FROM Category c WHERE c.cateId = :cateId"),
@NamedQuery(name = "Category.findByCateName", query = "SELECT c FROM Category c WHERE c.cateName = :cateName")})
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "cate_id")
private Integer cateId;
@Basic(optional = false)
@Column(name = "cate_name")
private String cateName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "category")
private List<SubCat> subCatList;
public Category() {
}
public Category(Integer cateId) {
this.cateId = cateId;
}
public Category(Integer cateId, String cateName) {
this.cateId = cateId;
this.cateName = cateName;
}
public Integer getCateId() {
return cateId;
}
public void setCateId(Integer cateId) {
this.cateId = cateId;
}
public String getCateName() {
return cateName;
}
public void setCateName(String cateName) {
this.cateName = cateName;
}
public List<SubCat> getSubCatList() {
return subCatList;
}
public void setSubCatList(List<SubCat> subCatList) {
this.subCatList = subCatList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (cateId != null ? cateId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Category)) {
return false;
}
Category other = (Category) object;
if ((this.cateId == null && other.cateId != null) || (this.cateId != null && !this.cateId.equals(other.cateId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.entity.Category[cateId=" + cateId + "]";
}
}
Second is SubCategory
, it represent the child menu items.
@Entity
@Table(name = "sub_cat")
@NamedQueries({
@NamedQuery(name = "SubCat.findAll", query = "SELECT s FROM SubCat s"),
@NamedQuery(name = "SubCat.findBySubcatid", query = "SELECT s FROM SubCat s WHERE s.subcatid = :subcatid"),
@NamedQuery(name = "SubCat.findBySubcatName", query = "SELECT s FROM SubCat s WHERE s.subcatName = :subcatName")})
public class SubCat implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "subcatid")
private Integer subcatid;
@Basic(optional = false)
@Column(name = "subcat_name")
private String subcatName;
@JoinColumn(name = "cat_parent", referencedColumnName = "cate_id")
@ManyToOne(optional = false)
private Category category;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "subCat")
private List<Items> itemList;
public SubCat() {
}
public SubCat(Integer subcatid) {
this.subcatid = subcatid;
}
public SubCat(Integer subcatid, String subcatName) {
this.subcatid = subcatid;
this.subcatName = subcatName;
}
public Integer getSubcatid() {
return subcatid;
}
public void setSubcatid(Integer subcatid) {
this.subcatid = subcatid;
}
public String getSubcatName() {
return subcatName;
}
public void setSubcatName(String subcatName) {
this.subcatName = subcatName;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public List<Items> getItemList() {
return itemList;
}
public void setItemList(List<Items> itemList) {
this.itemList = itemList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (subcatid != null ? subcatid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof SubCat)) {
return false;
}
SubCat other = (SubCat) object;
if ((this.subcatid == null && other.subcatid != null) || (this.subcatid != null && !this.subcatid.equals(other.subcatid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.entity.SubCat[subcatid=" + subcatid + "]";
}
}
Two class have many to one relationship. I want to display them on my a JSF 2.0/Facelets webpage like:
Category 1
|
----> Sub_Category 1
|
----> Sub_Category 2
Category 2
|
----> Sub_Category 3
|
----> Sub_Category 4
. . .
How can I do this?