JPA CRUD

= INSERT, SELECT, UPDATE, DELETE

[강의] 자바 ORM 표준 JPA 프로그래밍 - 기본편


INSERT

  • persist( ) 사용
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
32
33
34
35
36
package com.hellojpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class JpaMain {
public static void main(String[] args) {

EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPABOOK");

EntityManager em = emf.createEntityManager();

// 트랜잭션
EntityTransaction tx = em.getTransaction();
tx.begin();

try {
Member member = new Member();
member.setId(1L);
member.setName("HelloA");

// member 인스턴스에 들어있는 값을 em에 저장하다 = db에 삽입하다
em.persist(member);

tx.commit();
} catch (Exception e) {
tx.rollback();
} finally {
em.close();
}
emf.close();
}

}

SELECT

  • find( ) 사용
1
2
3
4
5
6
7
try {
Member findMember = em.find(Member.class, 1L);
System.out.println("findMember.id = " + findMember.getId());
System.out.println("findMember.name = " + findMember.getName());

tx.commit();
} catch (Exception e) {
  • 결과

Untitled

UPDATE

  • find( ) 사용
  • setter 함수 사용
1
2
3
4
5
6
try {
Member findMember = em.find(Member.class, 1L);
findMember.setName("HelloJPA");

tx.commit();
} catch (Exception e) {

DELETE

  • find( ) 사용
  • remove( ) 사용
1
2
3
4
5
6
try {
Member findMember = em.find(Member.class, 1L);
em.removd(findMember);

tx.commit();
} catch (Exception e) {

SELECT 주의할 점

  • 그냥 find( ) 사용으로는 불가능한 것도 있다
  • 그럴 때 사용하는 것이 JPQL

JPQL 개념

  • 객체 대상으로 작동하는 쿼리
  • 객체 지향 쿼리

Untitled

Untitled


JPQL 사용법

Untitled

SELECT

  • JPQL예시 코드
1
2
3
4
5
6
7
try {
List<Member> result = em.createQuery("select m from Member as m", Member.class)
.getResultList();

for (Member member : result) {
System.out.println("Member.name = " + member.getName());
}
  • 결과

Untitled

Author

minkuen

Posted on

2023-06-07

Updated on

2023-06-07

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.