Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- gradle
- JavaScript
- error
- Spring Boot
- Jenkins
- nginx
- grpc
- subnetmask
- MySQL
- jenkins install
- IntelliJ
- 리액트
- vue.js
- jenkins github 연동
- REACT
- jenkins jdk
- 리눅스
- jenkins 설치
- jpa
- Linux
- jenkins maven
- MongoDB
- spring
- Jenkins Pipeline
- docker network
- java
- CI/CD
- jenkins github
- grafana
- Docker
Archives
- Today
- Total
뭐든 즐기면서 ;)
JPA persistence.xml / JPA DB설정 본문
728x90
1. Entity 설정
@Entity: JPA가 관리할 객체(DB와 매핑될 객체)
@id: DB PK와 매핑할 필드

2. persistence.xml 설정 ( /META-INF/persistence.xml 위치 )

- persistence.xml 내용
* 데이터베이스 방언(dialect) = 각각의 DB가 제공하는 SQL 문법과 함수가 조금씩 다름. JPA는 특정 DB에 종속적이지 않은 기술. 이를 설정하는 방법은 persistence.xml의
<properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> </properties>
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="study">
<!-- Implements JPA,Hibernate -->
<!--<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>-->
<!-- Entities -->
<class>com.study.jpa.entity.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="data~secret!" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/study_db?autoReconnect=true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<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="update" />-->
</properties>
</persistence-unit>
</persistence>
JPA 구동 순서 : persistence.xml 정보 조회 -> Persistence 클래스를 통해 EntityManagerFactory 생성 -> 트랜잭션 단위로 EntityManager가 생성됨.
3. 코드 작성


// nativeJpaTest 메소드를 봐주세요.
// 해당 메소드를 junit을 통해 실행시켜봅니다.
public class SampleTest {
@Test
public void console() {
System.out.println("Sample class console 메소드 작동");
}
@Test
@Transactional
public void nativeJpaTest() {
// createEntityManagerFactory 함수의 매개변수"study"가
// persistence.xml파일의 <persistence-unit name="study">와 매칭됩니다.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("study");
EntityManager em = emf.createEntityManager();
// JPA의 모든 활동은 transaction 안에서 실행되야 함.
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
User user = new User();
user.setEmail("byEmSettingEmail");
user.setPassword("124455");
user.setName("userName-");
user.setBirth(LocalDate.now());
user.setCell_phone("01098649402");
user.setGender(Gender.FEMALE);
em.persist(user);
tx.commit();
} catch(Exception e) {
tx.rollback();
} finally {
em.close(); // 사용 후에는 꼭 close
}
emf.close();
}
}
4. 테스트 코드 실행

5. insert



* EntityFactoryManager는 하나만 생성해서 APP 전체에서 공유해야 함.
* EntityManager는 쓰레드간에 공유하면 안 됨. (트랜잭션마다 생성하고 close해야 함)
728x90
'BACK > Spring Boot & JPA' 카테고리의 다른 글
Spring Security 기본 설정 (0) | 2023.04.28 |
---|---|
Spring Boot + Gradle Multi Module (0) | 2022.11.18 |
Spring Boot(Gradle) + MySQL + JPA(Hibernate) [3] - Spring Data JPA 설정 2 (0) | 2021.10.27 |
Spring Boot(Gradle) + MySQL + JPA(Hibernate) [2] - Spring Data JPA 설정 1 (0) | 2021.10.25 |
Spring Boot(Gradle) + MySQL + JPA(Hibernate) [1] - Spring Boot 설정 (0) | 2021.10.24 |
Comments