hibernate一對多 操作(增,刪,改,查)

問題:
在Student中添屬性SC(SC表示課程表);
Student student = new Student();
SC sc = new SC();
student.setSC(sc);
在hibernate配置文件中設置cascade=true,

getHibernateTemplate.save(student);
更新與插入類試;
getHibernateTemplate.update(student);

解決方案:

    1. 學生Student.hbm.xml:
    2. <hibernate-mapping>
    3.     <class name="com.ssh.Student" table="student" schema="dbo" catalog="sshcourse">
    4.         <id name="stuId" type="integer">
    5.             <column name="stu_id" />
    6.             <generator class="native" />
    7.         </id>
    8.         <property name="stuName" type="string">
    9.             <column name="stu_name" length="50" />
    10.         </property>
    11.         <property name="stuPwd" type="string">
    12.             <column name="stu_pwd" length="50" />
    13.         </property>
    14.         <set name="courses"
    15.         table="course_student_table"
    16.         cascade="save-update"
    17.         inverse="false" 
    18.         lazy="false">
    19.         <key column="stu_id"></key>
    20.         <many-to-many class="com.ssh.Course" column="cou_id"></many-to-many>
    21.         </set>
    22.     </class>
    23. </hibernate-mapping>
    24. 課程Course.hbm.xml:
    25. <hibernate-mapping>
    26.     <class name="com.ssh.Course" table="course" schema="dbo" catalog="sshcourse">
    27.         <id name="couId" type="integer">
    28.             <column name="cou_id" />
    29.             <generator class="native" />
    30.         </id>
    31.         <property name="couName" type="string">
    32.             <column name="cou_name" length="50" />
    33.         </property>
    34.         <property name="couDis" type="string">
    35.             <column name="cou_dis" length="50" />
    36.         </property>
    37.         <set name="students"
    38.         table="course_student_table"
    39.         cascade="save-update"
    40.         inverse="true">
    41.         <key column="cou_id"></key>
    42.         <many-to-many class="com.ssh.Student" column="stu_id"></many-to-many>
    43.         </set>
    44.     </class>
    45. </hibernate-mapping>
    他們之間是通過course_student_table學生課程表關聯的
  1. cascade=true表設置級聯,即插入學生信息同時也插入選課信息。
  2. <set name="courses"
  3.         table="course_student_table"
  4.         cascade="save-update"
  5.         inverse="false" 
  6.         lazy="false">
  7.         <key column="stu_id"></key>
  8.         <many-to-many class="com.ssh.Course" column="cou_id"></many-to-many>
  9. </set>
  10. <set name="students"
  11.         table="course_student_table"
  12.         cascade="save-update"
  13.         inverse="true">
  14.         <key column="cou_id"></key>
  15.         <many-to-many class="com.ssh.Student" column="stu_id"></many-to-many>
  16. </set>

實例:

  1. inverse="false" 表示爲主控方,主控方負責維護關聯關係,一般在一對多關係中,把多的一方設置爲inverse="false"。lazy="false表示是延遲加載,當爲true時,啓動延遲加載,如兩個關聯a,b不延遲加載時可能加載a時候,也把b加載了,但你可能沒用b,只用a了,這就造成了浪費,延遲加載時表示用到b時才加載。
  2. 刪除例子
  3. public ActionForward execute(ActionMapping mapping, ActionForm form,
  4. HttpServletRequest request, HttpServletResponse response) {
  5. // TODO Auto-generated method stub 
  6. CourseID courseid=(CourseID)form;
  7. Student student=studao.findById((Integer)(request.getSession().getAttribute("stuid")));
  8. if(courseid.getCourses().length>0)
  9. {
  10. for(int i=0;i<courseid.getCourses().length;i++)
  11. {
  12. Integer cid=new Integer(courseid.getCourses()[i]);
  13. Course course=coudao.findById(cid);
  14. student.getCourses().remove(course);
  15. System.out.println("this is test1 sdfsdfg");
  16. System.out.println(cid);
  17. }
  18. }
  19. else
  20. {
  21. System.out.println("null");
  22. }
  23.                 studao.delete(student);
  24. return mapping.findForward("success");
  25. }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章