SSh中 一对多关联外键添加,修改保存失败问题

来源:互联网 发布:马刺热火第六场数据 编辑:程序博客网 时间:2024/06/07 17:41

今天博主在做一个ssh项目时候,需要使用到一级分类和二级分类关联,然后是在保存二级分类的时候,二级分类的name可以正常保存,而二级分类关联的一级分类的cid不能插入老是为空。   憋了一天一夜终于把问题解决,,虽然不知道为撒,,但是个人感觉还是因为  关联问题的原因。。。首先建立各自的实体类:

public class Category implements Serializable{/** *  */private static final long serialVersionUID = 1L;private Integer cid;private String cname;// 一级分类中存放二级分类的集合:private Set<CategorySecond> categorySeconds = new HashSet<CategorySecond>();public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public Set<CategorySecond> getCategorySeconds() {return categorySeconds;}
public void setCategorySeconds(Set<CategorySecond> categorySeconds) {this.categorySeconds = categorySeconds;}}

二级分类的实体类:

public class CategorySecond {private Integer csid;private String csname;// 所属一级分类.存的是一级分类的对象.private Category category;// 配置商品集合private Set<Product> products = new HashSet<Product>();public Integer getCsid() {return csid;}public void setCsid(Integer csid) {this.csid = csid;}public String getCsname() {return csname;}public void setCsname(String csname) {this.csname = csname;}public Category getCategory() {return category;}public void setCategory(Category category) {this.category = category;}public Set<Product> getProducts() {return products;}public void setProducts(Set<Product> products) {this.products = products;}}

他们各自的配置文件如下:一级分类的配置文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="shop.category.vo.Category" table="category"><id name="cid"><generator class="native"/></id><property name="cname"/><!-- 配置二级分类的集合 ,每一次刷新之后都不变--><set  name="categorySeconds" inverse="true" lazy="false"  order-by="csid" cascade="all"  ><!-- 外键 --><key column="cid"/><one-to-many class="shop.categorysecond.vo.CategorySecond"/></set></class></hibernate-mapping>
二级分类的配置文件:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="shop.categorysecond.vo.CategorySecond" table="categorysecond"><id name="csid"><generator class="native"/></id><property name="csname"/><!-- 二级分类与一级分类的关联 ....多的一端,是many,name是类这一段的属性,columu找表里的属性--><many-to-one  name="category"  lazy="false" class="shop.category.vo.Category" column="cid"  ></many-to-one><!-- 二级分类与商品的关联 --><set name="products"><key column="csid"/><one-to-many class="shop.product.vo.Product"/></set></class></hibernate-mapping>
此处使用一对多的set集合这种形式进行关联,,然后查阅网上原因的时候,特意加了下面的主控方和被空方问题    inverse="true"这个加在一对多的   一的那一方。

关于这个问题,还是没有解决我的问题。

然后又怀疑是级联问题,特意把级联加了进去  cascade=“all”,结果问题还是没有解决。

这是我的action,

package shop.categorysecond.adminaction;import java.util.List;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.sun.org.apache.bcel.internal.generic.NEW;import shop.category.service.CategoryService;import shop.category.vo.Category;import shop.categorysecond.service.CategorySecondService;import shop.categorysecond.vo.CategorySecond;import shop.utils.PageBean;/** * 后台二级分类的对象 *  * @author feng * */public class AdminCategorySecondAction extends ActionSupport implements ModelDriven<CategorySecond> {/** *  */private CategorySecond categorySecond = new CategorySecond();@Overridepublic CategorySecond getModel() {// TODO Auto-generated method stubreturn categorySecond;}// 注入二级分类的serviceprivate CategorySecondService categorySecondService;public void setCategorySecondService(CategorySecondService categorySecondService) {this.categorySecondService = categorySecondService;}// 接受pageprivate Integer page;public void setPage(Integer page) {this.page = page;}//接受cid;;;;此处我也不知道为啥需要用一个这个才好使,,,因为在ad<span style="color:#ff0000;">/*private Integer cid;public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}*/</span>// 注入一级分类的Serviceprivate CategoryService categoryService;public void setCategoryService(CategoryService categoryService) {this.categoryService = categoryService;}// 查询二级分类的方法public String findAll() {PageBean<CategorySecond> pageBean = categorySecondService.findByPage(page);// 在service方法中我们已经获取到了pagebean 将pagebean放到值栈中ActionContext.getContext().getValueStack().set("pageBean", pageBean);return "findAll";}// 二级分类的添加页面:跳转public String addPage() {// 查询所有一级分类List<Category> cList = categoryService.findAll();// 把数据显示到页面的下拉列表中,保存到值栈ActionContext.getContext().getValueStack().set("cList", cList);// 页面跳转return "addPageSuccess";}// 保存二级分类的方法public String save() {<span style="color:#ff0000;">categorySecond.setCategory(categoryService.findByCid(cid));</span>categorySecondService.save(categorySecond);return "saveSuccess";}// 删除二级分类的方法:public String delete() {// 根据id查询二级分类:如果级联删除。先查询在删除,配置cascade。。。System.out.println(categorySecond.getCsid());categorySecond = categorySecondService.findByCsid(categorySecond.getCsid());categorySecondService.delete(categorySecond);return "deleteSuccess";}//编辑二级分类的方法public String edit(){//根据二姐分类id查询二级分类的对象categorySecond=categorySecondService.findByCsid(categorySecond.getCsid());//查询所有的一级分类List<Category> cList=categoryService.findAll();ActionContext.getContext().getValueStack().set("cList", cList);return "editPageSuccess";}// 修改二级分类的方法:public String update(){categorySecond.setCategory(categoryService.findByCid(cid));categorySecondService.update(categorySecond);return "updateSuccess";}}

  然后是我的添加页面:
<body><form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminCategorySecond_save.action" method="post" enctype="multipart/form-data"> <table cellSpacing="1" cellPadding="5" width="100%" align="center" bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0"><tr><td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4"height="26"><strong><STRONG>添加二级分类</STRONG></strong></td></tr><tr><td width="18%" align="center" bgColor="#f5fafe" class="ta_01">二级分类名称:</td><td class="ta_01" bgColor="#ffffff"><input type="text" name="csname" value="" id="userAction_save_do_logonName" class="bg"/></td><td width="18%" align="center" bgColor="#f5fafe" class="ta_01">所属的一级分类:</td><span style="color:#cc0000;"><td class="ta_01" bgColor="#ffffff"><select name="cid"><s:iterator value="cList" var="c"><option value="<s:property value="#c.cid"/>" ><s:property value="#c.cname"/> </option></s:iterator></select></td></span></tr><tr><td class="ta_01" style="WIDTH: 100%" align="center"bgColor="#f5fafe" colSpan="4"><button type="submit" id="userAction_save_do_submit" value="确定" class="button_ok">确定</button><FONT face="宋体">       </FONT><button type="reset" value="重置" class="button_cancel">重置</button><FONT face="宋体">       </FONT><INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/><span id="Label1"></span></td></tr></table></form></body>
dao层和service层一级配置文件和struts.xml经楼主检查没有错误,便不在贴出来了。

楼主的最终解决办法,可以看到在action中有一段用红色标记的注释掉的内容,开始是没有的,后来添加上,,就好使了。具体原因我也不知道为啥。

但是此处我还是有很多疑惑,希望各位大神为我解答:

1.在action中我是用modelDriven之后,本来可以用模型categorySecond直接接收数据的,但是这里为什么没有接受到一级分类中的cid,而是需要我自己去重新定义这样一个id,然后set,get呢。

0 0
原创粉丝点击