hyljava

          Hibernate中用逆轉(zhuǎn)工程實(shí)現(xiàn)多對(duì)多關(guān)系映射


          history中的c_id與s_id分別是外鍵。
          在hibernate中用myeclicpse逆轉(zhuǎn)工程實(shí)現(xiàn)的多對(duì)多


          將 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;
            }
           }
          測(cè)試類如下:

          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("張同學(xué)");
            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) 評(píng)論(0)  編輯  收藏 所屬分類: Hibernate

          主站蜘蛛池模板: 定州市| 江阴市| 东阿县| 芜湖市| 南靖县| 淮阳县| 连云港市| 兴山县| 察隅县| 科技| 高邑县| 黄石市| 阳东县| 屯昌县| 丰县| 桃园市| 西充县| 宝丰县| 明星| 沂南县| 祁连县| 新昌县| 奉节县| 锦州市| 辰溪县| 长武县| 循化| 策勒县| 建阳市| 兴文县| 噶尔县| 康乐县| 布拖县| 安龙县| 巴中市| 浠水县| 玉山县| 灵石县| 余庆县| 临清市| 页游|