๋์์ ๋ ๊ฐ์ ๊ธฐ์ง์์ ์์
๋์์ ๋ ๊ฐ์ ๊ธฐ์ง๋ก ์์
ํด์ผ ํ๋ ์ํฉ์ด ์ข
์ข
์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณ ์ฒ๋ฆฌํ๊ณ ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋๋ค.
๋๋ ๋ ์ผ๋ฐ์ ์
๋๋ค. ์ ํ๋ฆฌ์ผ์ด์
์๋ ์์
ํ ํ๋์ ๋ก์ปฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์์ผ๋ฉฐ SQL ๋ฐ์ดํฐ ์ฟผ๋ฆฌ ํ์์ ์ง์ํ๋ ๋ค์ํ ์๊ฒฉ ์์ค์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ์์
.
์ด๋ฌํ ์๊ฒฉ ๋ฐ์ดํฐ ์์ค์ ๊ฒฝ์ฐ ์ ํ๋ฆฌ์ผ์ด์
์ ๋น์ฆ๋์ค ๋ก์ง์ด ๋ ๋จ์ํด์ง๊ณ Java ๊ฐ์ฒด์ ๋ํด์๋ง ์๋ํ ์ ์๋๋ก Hibernate ๋งคํ์ ์ฌ์ฉ์ ์ ์ํ๋ ๊ฒ์ด ๋งค์ฐ ์ ์ฉํ ์ ์์ต๋๋ค.
์ด๋ฅผ ์ํํ๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ ์ฌ๋ฌ ๊ตฌ์ฑ ํ์ผ์ ๊ฐ๋ ๊ฒ์
๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
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์ ์ข
์์ฑ์ ์ฌ์ฉํ์ฌ ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋์ผํ ๋ฐฉ์์ผ๋ก ํ๋ก์ ํธ์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
hsqldb |
<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>
|
์ผ๋ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋์ผํ ๋ฐฉ์์ผ๋ก ์์
์ ๊ตฌ์ฑํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ํธ์๋ฅผ ์ํด ๋ช ๊ฐ์ง ์๋ฅผ ๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
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>
|
๋ ๊ตฌ์ฑ์ ์ฃผ์ ์ฐจ์ด์ ์ 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>
|
GO TO FULL VERSION