hello jpa

김강현·2023년 3월 17일
0

ORM-JPA

목록 보기
1/9

지금껏 DB 와 어플에서 쓰이는 객체를 연결시키느라 했던 수많은 노가다들...
모든 class마다 toMapForData, toJson, clone 등등을 만들며 지내던 시간들...

김영한 개발자님도 똑같았다고 하셨....

ORM 기술을 활용하면, 객체와 관계형DB 간에 했던 수많은 노가다, 고민들을 안해도 됨!

(1) intellij 로 maven 프로젝트 생성

데이터 베이스 : h2 (www.h2database.com)
[ 최고의 실습용 DB ]

Maven 으로 생성 (Gradle도 ㄱㅊ하지만)
(maven arch__ 말고, new project로 maven 설정하기!)

(2) pom.xml 에서 라이브러리 주입

<?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>jpa-basic</groupId>
<artifactId>ex1-hello-jpa</artifactId>
<version>1.0.0</version>
	<dependencies>
        <!-- JPA 하이버네이트 -->
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>5.3.10.Final</version>
        </dependency>
        <!-- H2 데이터베이스 -->
        <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
          <version>1.4.199</version>
      	</dependency>
        <!-- JAXB 라이브러리 -->
      	<dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
	</dependencies>
</project>

jpa 구현체 Hybernate 라이브러리 (5.3.10 final 버전)

  • 추후 spring boot가 지원해주는 버전도 생각해서 버전 down

h2 라이브러리 (1.4.199 버전)

  • 다운로드 받은것과 꼭 같은 버전이어야함.

(3) resources/META-INF/persistence.xml 에 jpa 세팅 주입

  • db 마다, 구현체마다 약간씩 다르기때문에, hybernate, h2를 사용한다고 알려주어야함. hibernate.dialect.h2 ~~
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="hello">
<properties>
<!-- 필수 속성 -->
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<!-- <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/> h2 구버전들-->
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test;MODE=MySQL"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<!-- 옵션 -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<!--<property name="hibernate.hbm2ddl.auto" value="create" />-->
</properties>
</persistence-unit>
</persistence>

(4) main에서 EntityManager 지정!

서버를 열고 닫을 때 딱 한번 createEntityManagerFactory !
서버 닫을 때, .close();

거의 모든 action 상황에서

EntityManager를 생성해주고, 끝날때 해당 엔티티매니저를 close

  • Transaction 속에서 값 변경하기!
profile
this too shall pass

0개의 댓글

Powered by GraphCDN, the GraphQL CDN