<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      37 Posts :: 64 Stories :: 21 Comments :: 0 Trackbacks

    Hibernate如何配置操作多個數據庫
     
    Question:   
     
    引用:   
    我現在在做一個項目,需要從兩個數據庫實例中存取數據,在用hibernate實現的過程中該如何配置和處理   
     
    --------------------------------------------------------------------------------   
     
    1、如果一個項目要從兩個乃至多個數據庫實例讀取數據,該如何處理?   
    2、如果只有一個數據庫oraA,為其建立建立oraA.hbm.xml以及hibernate.properties文件,該文件中的數據庫連接部分寫為:   
    ##  Oracle   
    hibernate.dialect  net.sf.hibernate.dialect.OracleDialect   
    hibernate.connection.driver_class  oracle.jdbc.driver.OracleDriver   
    hibernate.connection.username  username   
    hibernate.connection.password  password   
    hibernate.connection.url  jdbc:oracle:thin:@local:1521:ora   
     
    3、如有兩個oracle數據庫oraA和oraB,分別為其建立oraA.hbm.xml和oraB.hbm.xml映射文件,這樣數據庫中的各個表和實體持久化類就可以映射好,但是hibernate.properties中的數據庫配置部分該如何處理呢,怎樣才能同時連接上兩個數據庫實例呢?   
     
     
    如果用這個Configuration.configure(File  configFile)   
    configFile可以是不同的配置。   
     
    通過配置configFile可以得到每個數據庫的configuration,  也就是可以獲得每個數據庫實例的session;但是在hibernate.properties文件中怎么寫數據庫連接部分呢,一個數據庫的時候寫法是:hibernate.connection.url  jdbc:oracle:thin:@local:1521:ora   
     
    那如果有兩個數據庫實例呢,在hibernate.properties文件中怎么處理,總不會是寫成如下吧:   
    ##oracle   
    hibernate.dialect  net.sf.hibernate.dialect.OracleDialect   
    hibernate.connection.driver_class  oracle.jdbc.driver.OracleDriver   
    hibernate.connection.username  username   
    hibernate.connection.password  password   
    hibernate.connection.url  jdbc:oracle:thin:@local:1521:oraA   
    hibernate.connection.username  usernamet   
    hibernate.connection.password  passwordt   
    hibernate.connection.url  jdbc:oracle:thin:@local:1521:oraB     
     
     
    Answer:   
     
    寫兩個hibernate.properties,例如連接oraA的叫做hibernateA.properties,連接oraB的叫做hibernateB.properties。   
     
    hibernateA.properties內容如下:   
     
    引用:   
    hibernate.dialect  net.sf.hibernate.dialect.OracleDialect   
    hibernate.connection.driver_class  oracle.jdbc.driver.OracleDriver   
    hibernate.connection.username  username   
    hibernate.connection.password  password   
    hibernate.connection.url  jdbc:oracle:thin:@local:1521:oraA     
     
     
     
    hibernateB.properties內容如下:   
     
    引用:   
    hibernate.dialect  net.sf.hibernate.dialect.OracleDialect   
    hibernate.connection.driver_class  oracle.jdbc.driver.OracleDriver   
    hibernate.connection.username  usernamet   
    hibernate.connection.password  passwordt   
    hibernate.connection.url  jdbc:oracle:thin:@local:1521:oraB     
     
     
    程序里面:   
     
    代碼:   
    java代碼:     
     
    Configuration  conf_oraA  =  new  Configuration("/hibernateA.properties").addClass().......;   
     
    Configuration  conf_oraB  =  new  Configuration("/hibernateB.properties").addClass().......;   
     
    SessionFactory  sf_oraA  =  conf_oraA.buildSessionFactory();   
    SessionFactory  sf_oraB  =  conf_oraB.buildSessionFactory();   
     
    Session  s_oraA  =  sf_oraA.openSession();   
    Session  s_oraB  =  sf_oraB.openSession();   
     
    ...... 
      
     
    --------------------------------------------------------------- 
     
    更正一下 
    方案一: 
    寫兩個hibernate.cfg.xml 
    使用數據庫a的時候 
    Configuration  cfg_a=new  Configuration().configure(hibernate.cfg_a.xml); 
    使用數據庫b的時候 
    Configuration  cfg_b=new  Configuration().configure(hibernate.cfg_b.xml);

    usr/home/zk1007874/htdocs/upload_files/images


     
    方案二: 
    使用一個能操作兩個數據庫的賬號,在?.hbm.xml映射文件中的class標簽中指定catalog為數據庫名

    posted on 2009-09-22 11:07 xiachang88 閱讀(242) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产高清在线免费| 成人无遮挡裸免费视频在线观看 | 五月天婷亚洲天综合网精品偷| 亚洲欧洲综合在线| 国产1000部成人免费视频| 亚洲AV无码精品色午夜果冻不卡| 国产做国产爱免费视频| 伊人久久亚洲综合| 两个人看的www高清免费视频 | 91九色视频无限观看免费| 亚洲午夜精品一区二区| 亚洲免费人成视频观看| 亚洲狠狠成人综合网| 免费特级黄毛片在线成人观看| 精品韩国亚洲av无码不卡区| 亚洲av无码成人精品区| 中国一级特黄的片子免费| 亚洲情XO亚洲色XO无码| 91在线手机精品免费观看| 亚洲另类小说图片| 国产大片免费观看中文字幕| 黄网站在线播放视频免费观看| 亚洲精品岛国片在线观看| 国产无遮挡无码视频免费软件| 亚洲精品高清久久| 99久久免费精品国产72精品九九| 亚洲avav天堂av在线网毛片| 亚洲人成影院在线无码观看| 亚洲免费人成在线视频观看| 亚洲国产精品网站久久| 国产免费久久精品久久久| baoyu777永久免费视频| 亚洲日本国产精华液| 国产免费av片在线无码免费看| aaa毛片视频免费观看| 久久亚洲精品无码VA大香大香| 四虎成人免费网站在线| 男女拍拍拍免费视频网站| 亚洲欧洲久久精品| 亚洲午夜无码片在线观看影院猛| 色猫咪免费人成网站在线观看|