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

          Hibernate 級聯添加刪除

          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();
          ????????
          ????????
          //級聯添加
          ????????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)


          ????????
          ????????
          //級聯刪除
          ????????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 級聯添加刪除[未登錄]  回復  更多評論   

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

          # re: Hibernate 級聯添加刪除  回復  更多評論   

          2008-06-16 16:05 by G_G
          你參考下此文檔 第2點第一條
          Hibernate 數據庫設計_1
          主站蜘蛛池模板: 揭西县| 嵩明县| 龙胜| 新竹县| 连南| 沅陵县| 丹巴县| 神池县| 兴国县| 博兴县| 安吉县| 武安市| 河间市| 海原县| 蓝田县| 建瓯市| 泽库县| 崇阳县| 泰来县| 大姚县| 海宁市| 八宿县| 同江市| 大渡口区| 六安市| 故城县| 临邑县| 资中县| 凉山| 白城市| 黄山市| 桓台县| 隆回县| 兴业县| 六盘水市| 炉霍县| 新干县| 龙井市| 库伦旗| 周宁县| 张北县|