Struts2插入并更新子表和父表

时间:2012-11-18 10:48:51

标签: hibernate struts2 actioncontext

我使用Hibernate和Struts2构建一个小型网站

我有一个表格类别

Struture喜欢:

  

ID

     

类别名称
      Parentcatid
      ...

我的所有文件都是:

类别Pojo

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import javax.persistence.*;

@Entity
@Table(name = "Category")
public class Category implements Serializable {

    private static long serialVersionUID = -3308170321970658110L;
    private Integer idcategory;
    private String categoryname;    
    private int homepage;
    private String categorynameviet;
    private String description;
    private String url;
    private Category parentidcat;
    private List<Category> subcategory ;

    /**
     * @return the idcategory
     */
    @Id
    @GeneratedValue
    @Column(name="idcategory")
    public Integer getIdcategory() {
        return idcategory;
    }

    /**
     * @param idcategory the idcategory to set
     */
    public void setIdcategory(Integer idcategory) {
        this.idcategory = idcategory;
    }

    /**
     * @return the categoryname
     */
    @Column(name="categoryname")
    public String getCategoryname() {
        return categoryname;
    }

    /**
     * @param categoryname the categoryname to set
     */
    public void setCategoryname(String categoryname) {
        this.categoryname = categoryname;
    }


    /**
     * @return the homepage
     */
    @Column(name="homepage")
    public int getHomepage() {
        return homepage;
    }

    /**
     * @param homepage the homepage to set
     */
    public void setHomepage(int homepage) {
        this.homepage = homepage;
    }

    /**
     * @return the categorynameviet
     */
    @Column(name="categorynameviet")
    public String getCategorynameviet() {
        return categorynameviet;
    }

    /**
     * @param categorynameviet the categorynameviet to set
     */
    public void setCategorynameviet(String categorynameviet) {
        this.categorynameviet = categorynameviet;
    }

    /**
     * @return the description
     */
    @Column(name="description")
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the url
     */
    @Column(name="url")
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the parentidcat
     */
    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="parentcatid")
    public Category getParentidcat() {
        return parentidcat;
    }

    /**
     * @param parentidcat the parentidcat to set
     */
    public void setParentidcat(Category parentidcat) {
        this.parentidcat = parentidcat;
    }

    /**
     * @return the subcategory
     */
    @OneToMany(mappedBy="parentidcat")
    public List<Category> getSubcategory() {
        return subcategory;
    }

    /**
     * @param subcategory the subcategory to set
     */
    public void setSubcategory(List<Category> subcategory) {
        this.subcategory = subcategory;
    }



}

我的行动

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.dejavu.pirate.dao.CategoryDAO;
import org.dejavu.pirate.model.Category;


public class CategoryAdminAction extends ActionSupport {

    private static final long serialVersionUID = -738951644056447324L;
    private Category cat;
    private static CategoryDAO catDAO = new CategoryDAO();
    private List<Category> categoryList;
    private boolean ckhomepage;

    @Override
    public String execute() {
        return SUCCESS;
    }

    public String setUpForInsertOrUpdate() {
        getCategoryParent();
        if (cat != null && cat.getIdcategory() != null) {
            cat = catDAO.findCatById(cat.getIdcategory());
        }
        return "success";
    }

    public String InsertOrUpdateCategory() {
        int parentId = 0;
        if (ckhomepage == true) {
            cat.setHomepage(1);
        } else {
            cat.setHomepage(0);
        }


        if (cat.getUrl() == null) {
            cat.setCategorynameviet(cat.getCategoryname());
        }

        if (cat.getIdcategory() == null) {

            catDAO.addCategory(cat);
        } else {            
            catDAO.updateCategory(cat);
        }
        return SUCCESS;
    }

    public void getCategoryParent() {
        categoryList = catDAO.getAllCategory();
        Map session = ActionContext.getContext().getSession();
        session.put("getAllCategoryID", categoryList);
    }

    @SkipValidation
    public String getAllCategory() {
        categoryList = catDAO.getAllCategory();
        return SUCCESS;
    }

    public List<Category> getCategoryList() {
        return categoryList;
    }

    public void setCategoryList(List<Category> categoryList) {
        this.categoryList = categoryList;
    }

    /**
     * @return the catDAO
     */
    public CategoryDAO getCatDAO() {
        return catDAO;
    }

    /**
     * @param catDAO the catDAO to set
     */
    public void setCatDAO(CategoryDAO catDAO) {
        this.catDAO = catDAO;
    }

    public Category getCat() {
        return cat;
    }

    public void setCat(Category cat) {
        this.cat = cat;
    }

    public boolean isCkhomepage() {
        return ckhomepage;
    }

    public void setCkhomepage(boolean ckhomepage) {
        this.ckhomepage = ckhomepage;
    }
}

和我的表单摘要,仅用于下拉框:

<s:select name="cat.parentcatid" list="#session.getAllCategoryID" listKey="parentcatid" listValue="categoryname" headerValue="-- Select Category --" headerKey="0" />

我在更新类别时遇到问题,它可以加载一个类别列表但是选中的是headerValue而不是它的父名称!

2 个答案:

答案 0 :(得分:0)

试试这个:

@Id
@GeneratedValue
@Column(name="idcategory")
private Integer idcategory;

答案 1 :(得分:0)

我使用s的值属性解决了我的问题:选择

<s:select name="cat.parentcatid" list="#session.getAllCategoryID" listKey="parentcatid" listValue="categoryname" headerValue="-- Select Category --" headerKey="0" value="parentid"/>