CodeGym /Java Course /All lectures for KO purposes /์ตœ๋Œ€ ์ ˆ์ „ ๋ชจ๋“œ์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ๋‰˜์•™์Šค

์ตœ๋Œ€ ์ ˆ์ „ ๋ชจ๋“œ์— ๋Œ€ํ•œ ์œ ์šฉํ•œ ๋‰˜์•™์Šค

All lectures for KO purposes
๋ ˆ๋ฒจ 1 , ๋ ˆ์Šจ 872
์‚ฌ์šฉ ๊ฐ€๋Šฅ

๋™์‹œ์— ๋‘ ๊ฐœ์˜ ๊ธฐ์ง€์—์„œ ์ž‘์—…

๋™์‹œ์— ๋‘ ๊ฐœ์˜ ๊ธฐ์ง€๋กœ ์ž‘์—…ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์ข…์ข… ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์”๋‹ˆ๋‹ค.

๋˜๋Š” ๋” ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—๋Š” ์ž‘์—…ํ•  ํ•˜๋‚˜์˜ ๋กœ์ปฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์žˆ์œผ๋ฉฐ 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>
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION