posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Hibernate 級聯(lián)添加刪除

          Posted on 2008-04-02 10:07 G_G 閱讀(2094) 評論(2)  編輯  收藏 所屬分類: hibernate
          問題描述:實體類答案從屬于實體類問題。(一對多)
          ?效果-》
          ????/**
          ?????*?添加?問題?和?選項
          ?????*?
          @throws?Exception
          ?????
          */@Test
          ????
          public?void?testQu()?throws?Exception?{
          ????????Session?session?
          =?HibernateUtil.currentSession();
          ????????Transaction?tr?
          =?session.beginTransaction();
          ????????
          ????????
          //級聯(lián)添加
          ????????Set?options?=?new?HashSet();
          ????????Options?op1?
          =?new?Options();
          ????????op1.setName(
          "op1");
          ????????options.add(op1);
          ????????
          ????????
          ????????Options?op2?
          =?new?Options();
          ????????op2.setName(
          "op2");
          ????????options.add(op2);
          ????????
          ????????
          ????????Options?op3?
          =?new?Options();
          ????????op3.setName(
          "op3");
          ????????options.add(op3);
          ????????Problems?problems?
          =?new?Problems();
          ????????
          ????????problems.setName(
          "problem_1");
          ????????problems.setOptions(options);
          ????????problems.setTdesc(
          "tdesc");
          ????????problems.setType(
          1);
          ????????
          ????????Long?ll?
          =?(Long)session.save(problems);
          ????????
          ????????
          ????????System.out.println(ll);
          ????????tr.commit();
          ???????
          mysql> select * from options ;
          +----+------------+--------+------+---------+
          | id | problemsid | answer | name | visible |
          +----+------------+--------+------+---------+
          |? 1 |????????? 1 |?? NULL | op2? |?????? 0 |
          |? 2 |????????? 1 |?? NULL | op3? |?????? 0 |
          |? 3 |????????? 1 |?? NULL | op1? |?????? 0 |
          +----+------------+--------+------+---------+
          3 rows in set (0.00 sec)

          mysql> select * from problems ;
          +----+-----------+------+-------+------------+---------+
          | id | name????? | type | tdesc | questionid | visible |
          +----+-----------+------+-------+------------+---------+
          |? 1 | problem_1 |??? 1 | tdesc |?????? NULL |?????? 0 |
          +----+-----------+------+-------+------------+---------+
          1 row in set (0.00 sec)


          ????????
          ????????
          //級聯(lián)刪除
          ????????tr.begin();
          ????????????session.delete(?session.get(Problems.
          class,ll)?);
          ????????tr.commit();

          mysql> select * from problems ;
          Empty set (0.00 sec)

          mysql> select * from options ;
          Empty set (0.00 sec)?


          ????????HibernateUtil.closeSession();
          ????}

          Options類
          .......
          ??? /**
          ?????*?@hibernate.many-to-one?
          ?????*?????????cascade?=?"save-update"
          ?????*?????????column?=?"Problemsid"
          ?????*?????????class?=?"com.zhongqi.domain.Problems"
          ?????*?
          @return
          ?????
          */
          ????
          public?Problems?getProblems()?{
          ????????
          return?problems;
          ????}
          ............

          Problems 類
          ????/**
          ?????*?@hibernate.set
          ?????*?????????cascade="all-delete-orphan"
          ?????*?????????inverse?=?"false"
          ?????*???????? lazy?=?"true"
          ?????*?????????@hibernate.collection-key??column?=?"problemsid"
          ?????*?????????@hibernate.collection-one-to-many?class?=?"com.zhongqi.domain.Options"
          ?????*?
          @return
          ?????
          */
          ????
          public?Set?getOptions()?{
          ????????
          return?options;
          ????}




          評論

          # re: Hibernate 級聯(lián)添加刪除[未登錄]  回復  更多評論   

          2008-06-12 19:53 by jack
          在hibernate中為何刪除記錄時,回進行更新操作。

          # re: Hibernate 級聯(lián)添加刪除  回復  更多評論   

          2008-06-16 16:05 by G_G
          你參考下此文檔 第2點第一條
          Hibernate 數(shù)據(jù)庫設計_1
          主站蜘蛛池模板: 遂平县| 得荣县| 大城县| 渑池县| 伊宁市| 乌拉特前旗| 长子县| 木里| 黔江区| 微山县| 集贤县| 罗城| 石棉县| 包头市| 吴江市| 新乡县| 阳西县| 通道| 庐江县| 肇庆市| 临江市| 承德县| 搜索| 金平| 浙江省| 西乌珠穆沁旗| 噶尔县| 华坪县| 始兴县| 樟树市| 灌云县| 旬阳县| 秭归县| 土默特左旗| 乐亭县| 黄梅县| 奇台县| 辽阳市| 黄冈市| 镇原县| 宁海县|