知道美河 | 上传资料 | VIP申请 | 精品课程 | 资料搜索 | 问题反馈 | 会员手册 | 积分消费 | 积分充值 | 帐号保护
美河学习学习在线赞助VIP

美河学习在线(主站) eimhe.com

 找回密码
 建立账号
查看: 9158|回复: 7

[求助] 关于多对多修改数据的疑问(wolfuni SSH2)

[复制链接]
发表于 2007-1-31 00:45:27 | 显示全部楼层 |阅读模式
在夜色狼族(wolfuni)老师ssh2项目的基础上, 我作了修改添加和删出 student,  添加和删出 都工作, 唯有修改有问题。
我用了两种方法,
一种用的ActionForm, ok
另一种用的DynaActionForm, 可以修改student, 但是在同时会删出course_student_table的数据
这是hibernate sql 语句
Hibernate: update student set stu_name=?, stu_pwd=? where stu_id=?
Hibernate: delete from course_student_table where stu_id=?

1.  struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
    <form-beans >
    <form-bean name="loginForm" type="com.affecta.db.Student" />
    <form-bean name="courseIDForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="coursesID" type="java.lang.String[]" />
    </form-bean>
    <form-bean name="studentProfileForm" type="com.affecta.db.Student" />
    <form-bean name="stuProDynaForm" type="org.apache.struts.action.DynaActionForm">
      <form-property name="stu" type="com.affecta.db.Student" />
    </form-bean>

  </form-beans>

  <global-exceptions />
  <global-forwards >
    <forward
      name="home"
      path="/home.do"
      redirect="true" />

  </global-forwards>

  <action-mappings >
    <action
      attribute="loginForm"
      input="/form/login.jsp"
      name="loginForm"
      path="/login"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy">
      <forward
        name="relogin"
        path="/form/login.jsp"
        redirect="true" />
      <forward
        name="showMyCourse"
        path="/showMyCourse.do"
        redirect="true" />
      <forward
        name="showAllCourses"
        path="/showAllCourses.do"
        redirect="true" />
      <forward
        name="home"
        path="/home.jsp"
        redirect="true" />
    </action>
    <action path="/showAllCourses" type="org.springframework.web.struts.DelegatingActionProxy">
      <forward
        name="showall"
        path="/form/showCourses.jsp"
        redirect="true" />
    </action>
    <action
      attribute="courseIDForm"
      input="/form/showCourses.jsp"
      name="courseIDForm"
      path="/selectCourse"
      type="org.springframework.web.struts.DelegatingActionProxy">
      <forward
        name="showMyCourse"
        path="/showMyCourse.do"
        redirect="true" />
    </action>
    <action path="/showMyCourse" type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="showMyCouList" path="/form/showMyCourse.jsp" />
    </action>
    <action
      attribute="studentProfileForm"
      input="/form/studentProfile.jsp"
      name="studentProfileForm"
      parameter="do"
      path="/studentProfile"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy">
      <forward
        name="editStudentProfile"
        path="/form/editStudentProfile.jsp"
        redirect="true" />
      <forward
        name="login"
        path="/form/login.jsp"
        redirect="true" />
      <forward
        name="viewStudentProfile"
        path="/viewStudentProfile.jsp"
        redirect="true" />
    </action>
    <action forward="/home.jsp" path="/home" />
    <action
      attribute="stuProDynaForm"
      input="/form/stuForm.jsp"
      name="stuProDynaForm"
      parameter="do"
      path="/stuProDyna"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="viewStuPro" path="/viewStuPro.jsp" />
      <forward name="editStuPro" path="/form/stuForm.jsp" redirect="false"/>
      <forward name="login" path="/form/login.jsp" redirect="true" />
    </action>
</action-mappings>
  <message-resources parameter="com.affecta.ui.ApplicationResources" />
   <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
  </plug-in>
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
  </plug-in>
  
</struts-config>

***********************************************************************
2. action 文件

        public ActionForward saveStu(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) {
               
            DynaActionForm stuForm = (DynaActionForm) form;// TODO Auto-generated method stub
            String stuID= request.getParameter("stu.stuId");
            String doString = request.getParameter("do");
            
          
            System.out.println((Student)stuForm.get("stu"));
            if(stuID!=null){
          
                   studao.update((Student)stuForm.get("stu"));
               
                return viewStu(mapping,form,request,response);
                }else{
                studao.save((Student)stuForm.get("stu"));
                System.out.println("login");
                return mapping.findForward("login");
                }
                //return null;
        }

请大家看一看, 到底是什么问题, 如何需要代码的, 请告诉我,我找空间上传。
多谢!

如果我在
public ActionForward saveStu(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response) {
    DynaActionForm stuForm = (DynaActionForm) form;// TODO Auto-generated method stub
   String stuID= request.getParameter("stu.stuId");
   String doString = request.getParameter("do");
   if(stuID!=null){
                Student student = studao.studao.findById(stuID);
                student.setStuName(((Student)stuForm.get("stu")).getStuName());
                student.setStuPwd(((Student)stuForm.get("stu")).getStuPwd());
                studao.update(student);

                return viewStu(mapping,form,request,response);
                }else{
                studao.save((Student)stuForm.get("stu"));
                System.out.println("login");
                return mapping.findForward("login");
                }
                //return null;
        }

改了以后, 就没有
Hibernate: delete from course_student_table where stu_id=?
执行。但是这样太麻烦了, 如果student 有好多属性, 要增加好多代码, 不知道大家有什么好方法没有?

[ 本帖最后由 owenhuan 于 2007-1-31 11:42 编辑 ]
发表于 2007-1-31 08:40:07 | 显示全部楼层
 楼主| 发表于 2007-1-31 08:49:06 | 显示全部楼层
Course.hbm.xml

<hibernate-mapping>
    <class name="com.affecta.db.Course" table="course" >
        <id name="couId" type="java.lang.Integer">
            <column name="cou_id" />
            <generator class="native" />
        </id>
        <property name="couName" type="java.lang.String">
            <column name="cou_name" length="20" not-null="true" />
        </property>
        <property name="couDis" type="java.lang.String">
            <column name="cou_dis" length="100" not-null="true" />
        </property>
         <set name="students"
                        table="course_student_table"
                        cascade="save-update"
                        inverse="true">
                        <key column="cou_id"></key>
                        <many-to-many class="com.affecta.db.Student" column="stu_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>
 楼主| 发表于 2007-1-31 08:49:52 | 显示全部楼层
Student.hbm.xml

<hibernate-mapping>
    <class name="com.affecta.db.Student" table="student">
        <id name="stuId" type="java.lang.Integer">
            <column name="stu_id" />
            <generator class="native" />
        </id>
        <property name="stuName" type="java.lang.String">
            <column name="stu_name" length="20" not-null="true" />
        </property>
        <property name="stuPwd" type="java.lang.String">
            <column name="stu_pwd" length="20" not-null="true" />
        </property>
        <set name="courses"
                        table="course_student_table"
                        cascade="save-update"
                        inverse="false"
                        lazy="false">
                        <key column="stu_id"></key>
                        <many-to-many class="com.affecta.db.Course" column="cou_id"></many-to-many>
        </set>
     </class>
</hibernate-mapping>
发表于 2007-1-31 11:25:51 | 显示全部楼层
student=(Student)stuForm.get("stu");
这样就不用对里面的属性一个一个赋值了。。。

还有一点就是我不知道你的id是怎么过来的。。。

我现在知道的就是session.save-update(student);
如果你执行这个方法。hibernate会自动检查你的student实例。如果id为空它会自动为你执行save方法。如果不为空则自动执行update方法。。
所以你不用判断id。。。

ps:当然判断不判断还看你的id是怎么来的了。呵呵。。。。

[ 本帖最后由 adamed 于 2007-1-31 11:54 编辑 ]
发表于 2007-1-31 11:45:35 | 显示全部楼层
不知道你不使用动态form的时候是否更新删除正常。。。。

[ 本帖最后由 adamed 于 2007-1-31 12:08 编辑 ]
 楼主| 发表于 2007-1-31 22:37:56 | 显示全部楼层
是正常的
使用动态form, 两种情况
1. saveStu  function
  if(stuID!=null){
         
                   studao.update((Student)stuForm.get("stu"));
               
                return viewStu(mapping,form,request,response);
                }
hibernate sql:
Hibernate: update student set stu_name=?, stu_pwd=? where stu_id=?
Hibernate: delete from course_student_table where stu_id=?


2. saveStu  function
if(stuID!=null){
                Student student = studao.studao.findById(stuID);
                student.setStuName(((Student)stuForm.get("stu")).getStuName());
                student.setStuPwd(((Student)stuForm.get("stu")).getStuPwd());
                studao.update(student);

                return viewStu(mapping,form,request,response);
                }
只有 Hibernate: update student set stu_name=?, stu_pwd=? where stu_id=?
没有 Hibernate: delete from course_student_table where stu_id=?


但是这两种更新student 都正常, 惟一的是第一种会同时会删出course_student_table的数据, 而第二种不会。
不知道什么原因!


[ 本帖最后由 owenhuan 于 2007-1-31 22:51 编辑 ]
发表于 2007-3-23 08:54:52 | 显示全部楼层
您需要登录后才可以回帖 登录 | 建立账号

本版积分规则

 
QQ在线咨询

QQ|小黑屋|手机版|Archiver|美河学习在线 ( 浙网备33020302000026号 )

GMT+8, 2025-7-12 17:44

Powered by Discuz!

© 2001-2025 eimhe.com.

快速回复 返回顶部 返回列表