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

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

          Posted on 2008-04-02 10:07 G_G 閱讀(2102) 評論(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)添加刪除[未登錄]  回復(fù)  更多評論   

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

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

          2008-06-16 16:05 by G_G
          你參考下此文檔 第2點第一條
          Hibernate 數(shù)據(jù)庫設(shè)計_1
          主站蜘蛛池模板: 三明市| 同德县| 如东县| 台中县| 长岭县| 翁牛特旗| 都昌县| 漯河市| 中超| 溧水县| 石台县| 宁强县| 石泉县| 武义县| 安溪县| 峡江县| 建始县| 佳木斯市| 绥中县| 和顺县| 娱乐| 普格县| 黄山市| 金秀| 乐昌市| 武平县| 洮南市| 丹棱县| 星子县| 筠连县| 泰州市| 揭东县| 明星| 宁国市| 苍梧县| 阿巴嘎旗| 分宜县| 上林县| 贡觉县| 堆龙德庆县| 芜湖县|