How to connect to multiple databases in Hibernate

后端 未结 6 805
长发绾君心
长发绾君心 2020-11-27 16:37

I am new bee to Hibernate and trying out things. One thing that seems to amuse all is how to connect to different databases? I have two questions here:

  1. If in t
6条回答
  •  借酒劲吻你
    2020-11-27 17:27

    It cannot be done using one hibernate configuration file. You need to have two configurations files for it.

    To configure mysql database

    hibernate-mysql.cfg.xml
    

    To configure oracle database

    hibernate-oracle.cfg.xml
    

    In Details, mysql configuration file be like this.

    
    
    
        
            false
            com.mysql.jdbc.Driver
            PASSWORD
            jdbc:mysql://localhost:3306/UR_DB_NAME
            USERNAME
            org.hibernate.dialect.MySQLDialect
            true
            
        
    
    

    In Details, oracle configuration file be like this.

    
    
    
        
            false
            oracle.jdbc.driver.OracleDriver
            PASSWORD
            jdbc:oracle:thin:UR DB NAME
            USERNAME
            org.hibernate.dialect.Oracle10gDialect
            true
            
        
    
    

    And code should be like this.

    mysql configuration

    private static SessionFactory sessionAnnotationFactory; 
    
    sessionAnnotationFactory = new Configuration().configure("hibernate-mysql.cfg.xml").buildSessionFactory();
    
    Session session = sessionAnnotationFactory.openSession();
    

    oracle sql configuration

    sessionAnnotationFactory = new Configuration().configure("hibernate-oracle.cfg.xml").buildSessionFactory();
    
    Session session = sessionAnnotationFactory.openSession()
    

提交回复
热议问题