views:

56

answers:

2

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?

A: 

You need a table with 3 columns:

id int unsigned not null - id of your menu item
name <whatever data type you need> not null - the name of your menu item to display
parent_id int unsigned default NULL - id of the parent menu item(if exists)

If parent_id is NULL, then it's a category. If parent_id is NOT NULL then it's a subcategory. You can use this to create as many menu-levels as you need.

Narf
Hello Narf i know it , but i dont know how to create Dyanmic menu with it, i create method with DataType is List<Category> and get all Category, one method get all Sub_category List<SubCat> and one method get all Item List<Items>, i know i must pass id of Catagory to SubCat for get list of SubCat and pass id of SubCat to items to get list of Items . But DataType return is List how can i pass it into Parameter of each item and subcat?
MYE
Well, I have no experience in Java, so I can't help you with that. I've only replied 'cause you've tagged your question with `database-design`.
Narf
A: 

As per the comments on the question, you seem to have trouble with displaying them in the view side, which is apparently JSF. As per your question history, you seem to be using JSF2 on Facelets as view technology.

You can just use ui:repeat to iterate over the categories and their subcategories. Assuming that you've a managed bean which returns a List<Category> by #{bean.categories}, here's an example:

<ul>
    <ui:repeat value="#{bean.categories}" var="category">
        <li>#{category.cateName}
            <h:panelGroup rendered="#{not empty category.subCatList}">
                <ul>
                    <ui:repeat value="#{category.subCatList}" var="subCat">
                        <li>#{subCat.subcatName}</li>
                    </ui:repeat>
                </ul>
            </h:panelGroup>
        </li>
    </ui:repeat>
</ul>
BalusC
@Balusc many thank you
MYE