hyljava

          Hibernate中用逆轉工程實現多對多關系映射


          history中的c_id與s_id分別是外鍵。
          在hibernate中用myeclicpse逆轉工程實現的多對多


          將 CourseDAO 中的save方法 改成:
           public void save(Course transientInstance) {
            log.debug("saving Course instance");
            Session session=null;
            Transaction tx=null;
            try {
             session=getSession();
             tx=session.beginTransaction();
             
             
             session.save(transientInstance);
             tx.commit();
             log.debug("save successful");
            } catch (RuntimeException re) {
             log.error("save failed", re);
             if(tx!=null){
              tx.rollback();
             }
             throw re;
            }
           }
          將 Student DAO 中的save方法 改成:

          public void save(Student transientInstance) {
            log.debug("saving Student instance");
            Session session=null;
            Transaction tx=null;
            try {
             session=getSession();
             tx=session.beginTransaction();
             
             session.save(transientInstance);
             log.debug("save successful");
            } catch (RuntimeException re) {
             log.error("save failed", re);
             if(tx!=null){
              tx.rollback();
             }
             throw re;
            }
           }
          將 HistoryDAO 中的save方法 改成:

          public void save(History transientInstance) {
            log.debug("saving History instance");
            Session session=null;
            Transaction tx=null;
            try {
             session=getSession();
             tx=session.beginTransaction();
             session.save(transientInstance);
             tx.commit();
             log.debug("save successful");
            } catch (RuntimeException re) {
             log.error("save failed", re);
             if(tx!=null){
              tx.rollback();
             }
             throw re;
            }
           }
          測試類如下:

          public class Test {

          public static void main(String[] args) {
              Course course=new Course();
              CourseDAO cdao=new CourseDAO();
            
            Student stu=new Student();
            StudentDAO sdao=new StudentDAO();
              
            History his=new History();
            HistoryId hid=new HistoryId();
            HistoryDAO hdao=new HistoryDAO();
            
            
              course.setCName("c++");
              course.setCTer("張三");
            
            stu.setSName("張同學");
            stu.setSAge(22);
            
            hid.setCourse(course);
            hid.setStudent(stu);
             
             his.setId(hid);
                cdao.save(course);
             sdao.save(stu);
             hdao.save(his);
            }

          }


          posted on 2012-05-13 18:04 何云隆 閱讀(249) 評論(0)  編輯  收藏 所屬分類: Hibernate

          主站蜘蛛池模板: 罗定市| 景谷| 余姚市| 成都市| 常山县| 大石桥市| 秦皇岛市| 柞水县| 潍坊市| 洪雅县| 特克斯县| 湛江市| 西宁市| 东乡族自治县| 内丘县| 芷江| 奎屯市| 保亭| 个旧市| 卫辉市| 通道| 嘉禾县| 原平市| 黄陵县| 河南省| 商南县| 通山县| 广南县| 阿拉善左旗| 兴城市| 隆德县| 垦利县| 双江| 阜阳市| 岢岚县| 莒南县| 日喀则市| 阜南县| 南阳市| 侯马市| 阳新县|