이 글은 김영한의 [자바 ORM 표준 JPA 프로그래밍 - 기본편]을 수강하며 정리한 글입니다.
👉 기본 환경
- Language: Java
- DB: H2 Database
- IDE: IntelliJ
Query: SELECT m.name, m.age FROM Member m
1. Query 타입 조회
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
37
38
39
40
41
42
|
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin(); // transaction 시작 선언
try {
Member member1 = new Member();
member1.setName("Member1");
em.persist(member1);
Member member2 = new Member();
member2.setName("Member2");
em.persist(member2);
// Scalar Type Projection
List list = em.createQuery("select distinct m.name, m.age from Member m")
.getResultList();
for(Object o : list){
Object[] result = (Object[]) o;
System.out.println("name: " + result[0]);
}
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
}
}
|
2. Object[] 타입 조회
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
37
38
39
40
41
|
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin(); // transaction 시작 선언
try {
Member member1 = new Member();
member1.setName("Member1");
em.persist(member1);
Member member2 = new Member();
member2.setName("Member2");
em.persist(member2);
// Scalar Type Projection
List<Object[]> list = em.createQuery("select distinct m.name, m.age from Member m")
.getResultList();
for(Object[] o : list){
System.out.println("name: " + o[0]);
}
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
}
}
|
3. new 명령어 조회
MemberDTO
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
|
public class MemberDTO {
private String name;
private int age;
public MemberDTO(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
|
Main
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
37
38
39
40
|
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin(); // transaction 시작 선언
try {
Member member1 = new Member();
member1.setName("Member1");
em.persist(member1);
Member member2 = new Member();
member2.setName("Member2");
em.persist(member2);
// Scalar Type Projection
List<MemberDTO> resultList = em.createQuery("select new jpql.MemberDTO(m.name, m.age) from Member m", MemberDTO.class)
.getResultList();
MemberDTO memberDTO = resultList.get(0);
System.out.println(memberDTO.getName());
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
}
}
|
⭐ 권장
- 단, 패키지명을 포함한 전체 클래스 명 입력이 필요(가독성 저하)
- 순서와 타입이 일치하는 생성자 필요
'Java > JPA' 카테고리의 다른 글
[JPA_Basic] 조인 (0) | 2023.09.23 |
---|---|
[JPA_Basic] 페이징 (0) | 2023.09.23 |
[JPA_Basic] 값 타입 컬렉션 (0) | 2023.09.19 |
[JPA_Basic] 값 타입과 불변 객체 (0) | 2023.09.14 |
[JPA_Basic] Cascade (0) | 2023.09.11 |