JPA(Java Persistence API)
Java ORM(Object Relational Mapping) 기술의 표준 사양
표준 사양은 인터페이스로 정의되어 해당 사양을 구현한 구현체는 따로 있다.
Hibernate ORM
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
spring:
h2:
console:
enabled: true
path: /h2
datasource:
url: jdbc:h2:mem:test
jpa:
hibernate:
ddl-auto: create # (1) 스키마 자동 생성
show-sql: true # (2) SQL 쿼리 출력
@Configuration 애너테이션을 추가하면 Spring에서 Bean 검색 대상인 Configuration 클래스로 간주해서 @Bean 애너테이션이 추가된 메서드를 검색한 후, 해당 메서드에서 리턴하는 객체를 Spring Bean으로 추가
@Getter
@Setter
@NoArgsConstructor
@Entity // (1)
public class Member {
@Id
@GeneratedValue // (2)
private Long memberId;
private String email;
public Member(String email) {
this.email = email;
}
}
private void example01() {
Member member = new Member("hgd@gmail.com");
// (1-3)
em.persist(member);
// (1-4)
Member resultMember = em.find(Member.class, 1L);
System.out.println("Id = " + resultMember.getMemberId() + ", email = " +
resultMember.getEmail());
}
Hibernate: drop table if exists member CASCADE
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table member (member_id bigint not null, email varchar(255), primary key (member_id))
Hibernate: call next value for hibernate_sequence
Id = 1, email = hgd@gmail.com
private void example02() {
// (2-2)
tx.begin();
Member member = new Member("hgd@gmail.com");
// (2-3)
em.persist(member);
// (2-4)
tx.commit();
// (2-5)
Member resultMember1 = em.find(Member.class, 1L);
System.out.println("Id = " + resultMember1.getMemberId() + ", email = " +
resultMember1.getEmail());
// (2-6)
Member resultMember2 = em.find(Member.class, 2L);
// (2-7)
System.out.println(resultMember2 == null);
}
Hibernate: drop table if exists member CASCADE
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table member (member_id bigint not null, email varchar(255), primary key (member_id))
Hibernate: call next value for hibernate_sequence
Hibernate: insert into member (email, member_id) values (?, ?)
Id = 1, email = hgd@gmail.com
Hibernate: select member0_.member_id as member_i1_0_0_, member0_.email as email2_0_0_ from member member0_ where member0_.member_id=?
true
private void example03() {
tx.begin();
Member member1 = new Member("hgd1@gmail.com");
Member member2 = new Member("hgd2@gmail.com");
em.persist(member1); // (3-1)
em.persist(member2); // (3-2)
tx.commit();
}
Hibernate: drop table if exists member CASCADE
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table member (member_id bigint not null, email varchar(255), primary key (member_id))
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: insert into member (email, member_id) values (?, ?)
Hibernate: insert into member (email, member_id) values (?, ?)
private void example04() {
tx.begin();
em.persist(new Member("hgd@gmail.com")); // (4-1)
tx.commit(); // (4-2)
tx.begin();
Member member1 = em.find(Member.class, 1L); // (4-3)
member1.setEmail("hgd1@gmail.com"); // (4-4)
tx.commit(); // (4-5)
}
(4-3) 영속성 컨텍스트의 1차 캐시에 저장된 객체가 있기 때문에 테이블이 아닌 영속성 컨텍스트에서 조회
(4-4) setter() 메서드로 정보 변경
(4-5) commit() 실행시 쓰기 지연 저장소에 등록된 UPDATE 쿼리가 실행
실행 로그
Hibernate: drop table if exists member CASCADE
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table member (member_id bigint not null, email varchar(255), primary key (member_id))
Hibernate: call next value for hibernate_sequence
Hibernate: insert into member (email, member_id) values (?, ?)
Hibernate: update member set email=? where member_id=
private void example05() {
tx.begin();
em.persist(new Member("hgd@gmail.com")); // (5-1)
tx.commit(); // (5-2)
tx.begin();
Member member = em.find(Member.class, 1L); // (5-3)
em.remove(member); // (5-4)
tx.commit(); // (5-5)
}
Hibernate: drop table if exists member CASCADE
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table member (member_id bigint not null, email varchar(255), primary key (member_id))
Hibernate: call next value for hibernate_sequence
Hibernate: insert into member (email, member_id) values (?, ?)
Hibernate: delete from member where member_id=?