Làm việc với hai cơ sở cùng một lúc

Khá thường xuyên có những tình huống khi bạn cần làm việc với hai cơ sở cùng một lúc. Ví dụ, bạn đọc dữ liệu từ một cơ sở dữ liệu, xử lý nó và ghi nó vào một cơ sở dữ liệu khác.

Hoặc thậm chí phổ biến hơn. Ứng dụng của bạn có một cơ sở dữ liệu cục bộ để làm việc và cũng có thể đọc dữ liệu từ nhiều nguồn từ xa khác nhau hỗ trợ định dạng truy vấn dữ liệu SQL. Ví dụ Excell.

Đối với các nguồn dữ liệu từ xa này, việc tùy chỉnh ánh xạ Hibernate của bạn có thể rất hữu ích để logic nghiệp vụ của ứng dụng có thể đơn giản hơn và chỉ hoạt động với các đối tượng Java.

Cách dễ nhất để làm điều này là có nhiều tệp cấu hình. Ví dụ, như thế này:

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>

Chà, tất cả mã khởi tạo Hibernate thực sự chỉ được sao chép:

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");

cơ sở trong bộ nhớ

Có DBMS, là những cơ sở dữ liệu hoàn toàn nằm trong bộ nhớ. Các DBMS như vậy thường được phân phối đơn giản dưới dạng thư viện và dễ dàng đưa vào một ứng dụng điển hình. Ví dụ, DBMS như vậy là H2 hoặc Hsqldb.

Bạn có thể thêm chúng vào dự án của mình theo cách giống như bất kỳ thư viện nào khác - sử dụng phần phụ thuộc trong Maven:

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

Và ngay lập tức một ví dụ về cơ sở thứ hai:

cơ sở dữ liệu H2
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.1.214</version>
    <scope>test</scope>
</dependency>

Bạn có thể định cấu hình làm việc với chúng giống như với cơ sở dữ liệu thông thường. Nhưng hãy đưa ra một vài ví dụ để thuận tiện:

hsqldb
<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>

Sự khác biệt chính giữa hai cấu hình là connection.url:

cơ sở dữ liệu 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>