同時與兩個基地合作

很多時候,您需要同時處理兩個基地。例如,您從一個數據庫讀取數據,對其進行處理並將其寫入另一個數據庫。

或者更常見。您的應用程序有一個本地數據庫可以使用,也可以從支持 SQL 數據查詢格式的各種遠程源讀取數據。例如 Excel。

對於這些遠程數據源,自定義 Hibernate 映射非常有用,這樣應用程序的業務邏輯就可以更簡單並且只使用 Java 對象。

最簡單的方法是擁有多個配置文件。例如,像這樣:

hibernate_oracle.cfg.xml
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">secret<property>
 
      <mapping resource="com/codegym/hibernate/multipledatabase/employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>
hibernate_mysql.cfg.xml
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/employee</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">secret</property>
 
      <mapping resource="com/codegym/hibernate/multipledatabase/employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>

好吧,所有 Hibernate 初始化代碼實際上只是重複的:

Configuration oracleCfg = new Configuration();
oracleCfg.configure("/com/codegym/hibernate/multipledatabase/hibernate_oracle.cfg.xml");
SessionFactory oracleSessionFactory = oracleCfg.buildSessionFactory();
Session oracleSession = oracleSessionFactory.openSession();

Configuration mysqlCfg = new Configuration();
mysqlCfg.configure("/com/codegym/hibernate/multipledatabase/hibernate_mysql.cfg.xml");
SessionFactory mySqlSessionFactory = mysqlCfg.buildSessionFactory();
Session mysqlSession = mySqlSessionFactory.openSession();

Employee emp = oracleSession.get(Employee.class, "E0087");
System.out.println("Employee retrived from Oracle Database");

Transaction tx = mysqlSession.beginTransaction();
mysqlSession.save(emp);
tx.commit();
System.out.println("Employee inserted in MySQL Database");

記憶中的基地

有 DBMS,它們是完全在內存中的數據庫。此類 DBMS 通常以庫的形式簡單分發,並且很容易包含在典型的應用程序中。例如,此類 DBMS 是 H2 或 Hsqldb。

您可以像添加任何其他庫一樣將它們添加到您的項目中——使用 Maven 中的依賴項:

數據庫
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.6.1</version>
	<scope>test</scope>
</dependency>

立即是第二個基地的例子:

H2數據庫
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.1.214</version>
    <scope>test</scope>
</dependency>

您可以按照與常規數據庫相同的方式配置它們。但為了方便起見,讓我們舉幾個例子:

數據庫
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbc.JDBCDriver</property>
        <property name="hibernate.connection.url">jdbc:hsqldb:mem:test</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <property name="hibernate.current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>

兩種配置的主要區別在於 connection.url:

H2數據庫
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.h2.Driver</property>
        <property name="hibernate.connection.url">jdbc:h2:mem:test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
	    <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <property name="hibernate.current_session_context_class">thread</property>
    </session-factory>
</hibernate-configuration>