티스토리 뷰

728x90

1. 하이버네이트에는 다양한 Annotation들이 존재한다. 기본적인 것 이외에 유용한 Annotation에 대한 시리즈이다.

  1-1 @ElementCollection로 Collection을 다룬다.

  1-2 @Embeddable, @Embedded에 대해서 설명한다.

  1-3 Enum을 사용하는 방법에 대해 작성한다.

  1-4 상속에 대한 다양한 경우에 대해서 설명한다.

 

2. 코드는 스프링을 사용하지 않고 Hibernate core 모듈만 가지고 작성한다.

  2-1 따라서 maven archetype quickstart 1.4를 사용하여 작성한다.

 

3. 데이터베이스는 MySql을 사용한다. 

  3-1 데이터베이스 생성은 테스트용으로 제공되는 하이버네이트의 hibernate.hbm2ddl.auto설정을 통해 자동 작성한다.

 

4. 설정 방법

  4-1 maven archetype quickstart 1.4로 프로젝트를 생성한다.

  4-2 pom.xml에 사용하기 원하는 자바 버전으로 설정을 변경하고

  4-3 하이버네이트 코어, MySql, lombok 라이브러리를 추가한다.

 

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>pe.pilseong</groupId>
  <artifactId>hibernatelist</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <name>hibernatelist</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>5.4.17.Final</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.20</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>
      
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

 

  4-4 하이버네이트 연결 설정

    4-4-0 하이버네이트에 연결에 대한 기본 설정이다.

    4-4-1 위치는 classpath root에 두면 된다. 기본적으로 src/main/java에 두면 된다.

    4-4-2 하이버네이트 설정은 dialect, show_sql, hbm2ddl, 정도가 있다. 

      4-4-2-1 dialect에 사용하는 MySql 버전에 적합한 클래스를 지정해야 한다.

      4-4-2-2 맞지 않으면 SQL실행시 DB 타입에러나 Query 수행 오류가 발생한다. 내 경우 MySql 8을 사용한다.

      4-4-2-3 hbmddl은 상황에 따라서 바꿔줘야 한다. create, update를 많이 사용한다.

 

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

  <session-factory>

    <!-- JDBC Database connection settings -->
      <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
      <property name="connection.url">jdbc:mysql://localhost:3306/advanced_hibernate?useSSL=false&amp;serverTimezone=Asia/Seoul</property>
      <property name="connection.username">root</property>
      <property name="connection.password">root</property>

    <!-- JDBC connection pool settings ... using built-in test pool -->
      <property name="connection.pool_size">1</property>

    <!-- Select our SQL dialect -->
      <property name="dialect">org.hibernate.dialect.MySQL8Dialect</property>

    <!-- Echo the SQL to stdout -->
      <property name="show_sql">true</property>
      
      <property name="hibernate.hbm2ddl.auto">update</property>

    <!-- Set the current session context -->
      <property name="current_session_context_class">thread</property>

  </session-factory>

</hibernate-configuration>

 

728x90
댓글