Hibernate Struts2插入并更新同一表的子表和父表
我使用Hibernate和Struts2构建了一个小网站 我有一个叫桌分类 结构类似: 身份证 类别名称Hibernate Struts2插入并更新同一表的子表和父表,hibernate,struts2,actioncontext,Hibernate,Struts2,Actioncontext,我使用Hibernate和Struts2构建了一个小网站 我有一个叫桌分类 结构类似: 身份证 类别名称 Parentcatid 我的所有文件都是: Pojo类 import java.io.Serializable; import java.util.List; import java.util.Set; import javax.persistence.*; @Entity @Table(name = "Category") public class Category implement
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 java.io.Serializable;
导入java.util.List;
导入java.util.Set;
导入javax.persistence.*;
@实体
@表(name=“Category”)
公共类类别实现可序列化{
私有静态长SerialVersionId=-3308170321970658110L;
私有整数类;
私有字符串categoryname;
私人互联网网页;
私有字符串类别名称;
私有字符串描述;
私有字符串url;
私有类parentidcat;
私有列表子类别;
/**
*@返回idcategory
*/
@身份证
@生成值
@列(name=“idcategory”)
公共整数getIdcategory(){
返回idcategory;
}
/**
*@param idcategory要设置的idcategory
*/
公共无效集合idcategory(整数idcategory){
this.idcategory=idcategory;
}
/**
*@返回类别名称
*/
@列(name=“categoryname”)
公共字符串getCategoryname(){
返回类别名称;
}
/**
*@param categoryname要设置的categoryname
*/
公共无效setCategoryname(字符串categoryname){
this.categoryname=categoryname;
}
/**
*@返回主页
*/
@栏目(name=“主页”)
公共int getHomepage(){
返回主页;
}
/**
*@param homepage要设置的主页
*/
公共网页(国际网页){
this.homepage=主页;
}
/**
*@返回类别名称
*/
@列(name=“CategoryNameVieta”)
公共字符串getCategoryNameVieta(){
返回类别名称;
}
/**
*@param CategoryNameVieto要设置的CategoryNameVieto
*/
public void setCategoryNameVieta(字符串categoryNameVieta){
this.categorynameVieta=categorynameVieta;
}
/**
*@返回描述
*/
@列(name=“description”)
公共字符串getDescription(){
返回说明;
}
/**
*@param description要设置的描述
*/
公共void集合描述(字符串描述){
this.description=描述;
}
/**
*@返回url
*/
@列(name=“url”)
公共字符串getUrl(){
返回url;
}
/**
*@param url要设置的url
*/
公共void setUrl(字符串url){
this.url=url;
}
/**
*@returntheparentidcat
*/
@manytone(cascade={CascadeType.ALL})
@JoinColumn(name=“parentcatid”)
公共类别getParentidcat(){
返回parentidcat;
}
/**
*@param parentidcat要设置的parentidcat
*/
公共无效setParentidcat(类别parentidcat){
this.parentidcat=parentidcat;
}
/**
*@返回子类别
*/
@OneToMany(mappedBy=“parentidcat”)
公共列表getSubcategory(){
返回子类别;
}
/**
*@param subcategory要设置的子类别
*/
公共无效集合子类别(列表子类别){
this.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;
}
}
import com.opensymphony.xwork2.ActionContext;
导入com.opensymphony.xwork2.ActionSupport;
导入java.util.List;
导入java.util.Map;
导入org.apache.struts2.interceptor.validation.SkipValidation;
导入org.dejavu.pirate.dao.CategoryDAO;
导入org.dejavu.pirate.model.Category;
公共类类别管理扩展了ActionSupport{
私有静态最终长serialVersionUID=-738951644056447324L;
私家猫;
私有静态CategoryDAO catDAO=新CategoryDAO();
私有列表分类列表;
私人网页;
@凌驾
公共字符串execute(){
回归成功;
}
公共字符串setUpForInsertOrUpdate(){
getCategoryParent();
if(cat!=null&&cat.getIdcategory()!=null){
cat=catDAO.findCatById(cat.getIdcategory());
}
返回“成功”;
}
公共字符串InsertOrUpdateCategory(){
int parentId=0;
if(ckhomepage==true){
第二类(1);
}否则{
目录设置(0);
}
if(cat.getUrl()==null){
cat.setCategoryName(cat.getCategoryname());
}
if(cat.getIdcategory()==null){
catDAO.addCategory(cat);
}否则{
catDAO.updateCategory(cat);
}
回归成功;
}
public void getCategoryParent(){
categoryList=catDAO.getAllCategory();
映射会话=ActionContext.getContext().getSession();
session.put(“getAllCategoryID”,类别列表);
}
@SkipValidation
公共字符串getAllCategory(){
categoryList=catDAO.getAllCategory();
回归成功;
}
公共列表getCategoryList(){
返回类别列表;
}
public void setCategoryList(列表类别列表){
this.categoryList=categoryList;
}
/**
*@归还猫刀
*/
公共类别dao getCatDAO(){
返回catDAO;
}
/**
*@param catDAO要设置的catDAO
*/
公共无效设置catDAO(类别DAO catDAO){
this.catDAO=catDAO;
}
公共类别getCat(){
返回猫;
}
公共无效集合类别(类别类别类别){
this.cat=cat;
}
公共布尔值isCkhomepage(){
返回主页;
}
公共无效设置主页(布尔设置主页){
this.ckhomepage=ckhomepage;
}
}
以及我的表单片段,仅用于下拉框:
<s:select name="cat.parentcatid" list="#session.getAllCategoryID" listKey="parentcatid" listValue="categoryname" headerValue="-- Select Category --" headerKey="0" />
我有一个问题,当更新类别,它可以加载一个类别列表,但选择是headerValue,而不是它的父名称 T
@Id
@GeneratedValue
@Column(name="idcategory")
private Integer idcategory;
<s:select name="cat.parentcatid" list="#session.getAllCategoryID" listKey="parentcatid" listValue="categoryname" headerValue="-- Select Category --" headerKey="0" value="parentid"/>