이 글은 김영한의 [자바 ORM 표준 JPA 프로그래밍 - 기본편]을 수강하며 정리한 글입니다.
👉 기본 환경
- Language: Java
- DB: H2 Database
- IDE: IntelliJ
상태 필드: 단순히 값을 저장하기 위한 필드
⭐ 경로 탐색의 끝, 탐색 X
⌨️ 코드
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) {
// 생략
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
member.setType(MemberType.ADMIN);
em.persist(member);
em.flush();
em.clear();
String query = "select m.name from Member m";
List<String> members = em.createQuery(query, String.class)
.getResultList();
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
// 트랜잭션 단위로 관리되는 Entity Manager는 Tx가 종료되면 close가 되어야하지만, emf는 Application 종료 시, close되어야 함
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
|
Hibernate:
/* select
m.name
from
Member m */ select
member0_.name as col_0_0_
from
Member member0_
|
연관 필드: 연관 관계를 위한 필드
- 단일 값 연관 필드: @ManyToOne, @OneToOne, 대상이 Entity
⭐ 묵시적 내부 조인 발생, 탐색 O
⌨️ 코드
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) {
// 생략
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
member.setType(MemberType.ADMIN);
em.persist(member);
em.flush();
em.clear();
String query = "select m.team from Member m";
List<Team> members = em.createQuery(query, Team.class)
.getResultList();
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
// 트랜잭션 단위로 관리되는 Entity Manager는 Tx가 종료되면 close가 되어야하지만, emf는 Application 종료 시, close되어야 함
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Hibernate:
/* select
m.team
from
Member m */ select
team1_.id as id1_3_,
team1_.name as name2_3_
from
Member member0_
inner join
Team team1_
on member0_.TEAM_ID=team1_.id
|
내부 Team Entity를 조회하기 위해 묵시적 Join 발생
⌨️ 코드
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) {
// 생략
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
member.setType(MemberType.ADMIN);
em.persist(member);
em.flush();
em.clear();
String query = "select m.team.name from Member m";
List<String> members = em.createQuery(query, String.class)
.getResultList();
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
// 트랜잭션 단위로 관리되는 Entity Manager는 Tx가 종료되면 close가 되어야하지만, emf는 Application 종료 시, close되어야 함
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Hibernate:
/* select
m.team.name
from
Member m */ select
team1_.name as col_0_0_
from
Member member0_ cross
join
Team team1_
where
member0_.TEAM_ID=team1_.id
|
내부 Entity로부터 재 탐색 가능
연관 필드: 연관 관계를 위한 필드
- 컬렉션 값 연관 필드: @OneToMany, @ManyToMany, 대상이 Collection
⭐ 묵시적 내부 조인 발생, 탐색 X
⭐⭐ FROM 절에서 명시적 조인을 통해 별칭을 얻으면 별칭을 통해 탐색 가능
⌨️ 코드
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) {
// 생략
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
member.setType(MemberType.ADMIN);
em.persist(member);
em.flush();
em.clear();
String query = "select t.members from Team t";
List<Collection> members = em.createQuery(query, Collection.class)
.getResultList();
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
// 트랜잭션 단위로 관리되는 Entity Manager는 Tx가 종료되면 close가 되어야하지만, emf는 Application 종료 시, close되어야 함
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Hibernate:
/* select
t.members
from
Team t */ select
members1_.id as id1_0_,
members1_.age as age2_0_,
members1_.name as name3_0_,
members1_.TEAM_ID as team_id5_0_,
members1_.type as type4_0_
from
Team team0_
inner join
Member members1_
on team0_.id=members1_.TEAM_ID
|
⌨️ 코드
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) {
// 생략
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
member.setType(MemberType.ADMIN);
em.persist(member);
em.flush();
em.clear();
String query = "select t.members.name from Team t";
List<String> members = em.createQuery(query, String.class)
.getResultList();
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
// 트랜잭션 단위로 관리되는 Entity Manager는 Tx가 종료되면 close가 되어야하지만, emf는 Application 종료 시, close되어야 함
}
}
|
🖨️ 발생한 오류
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
|
java.lang.IllegalArgumentException: org.hibernate.QueryException: illegal attempt to dereference collection [team0_.id.members] with element property reference [name] [select t.members.name from jpql.Team t]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:725)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:816)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
at jpql.Main.main(Main.java:139)
Caused by: org.hibernate.QueryException: illegal attempt to dereference collection [team0_.id.members] with element property reference [name] [select t.members.name from jpql.Team t]
at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:220)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:113)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:73)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:155)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716)
... 3 more
Caused by: org.hibernate.QueryException: illegal attempt to dereference collection [team0_.id.members] with element property reference [name]
at org.hibernate.hql.internal.ast.tree.DotNode$1.buildIllegalCollectionDereferenceException(DotNode.java:59)
at org.hibernate.hql.internal.ast.tree.DotNode.checkLhsIsNotCollection(DotNode.java:639)
at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:245)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:114)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:109)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:104)
at org.hibernate.hql.internal.ast.tree.DotNode.resolveSelectExpression(DotNode.java:768)
at org.hibernate.hql.internal.ast.HqlSqlWalker.resolveSelectExpression(HqlSqlWalker.java:1065)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2319)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2256)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1518)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:597)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:325)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:273)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:272)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192)
... 9 more
|
Hibernate는 컬렉션에 대한 직접적인 접근을 허용하지 않음
그러므로, FROM 절에서 명시적 조인을 통해 별칭을 얻으면 별칭을 통해 탐색
⌨️ 코드
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) {
// 생략
try {
Team team = new Team();
team.setName("Team");
em.persist(team);
Member member = new Member();
member.setName("Member");
member.setAge(10);
member.setTeam(team);
member.setType(MemberType.ADMIN);
em.persist(member);
em.flush();
em.clear();
String query = "select m.name from Team t join t.members m ";
List<String> members = em.createQuery(query, String.class)
.getResultList();
tx.commit(); // transaction 종료 후 commit
} catch (Exception e) {
tx.rollback(); // 문제가 생길 경우, rollback 진행
e.printStackTrace();
} finally {
em.close(); // tx에 문제가 생기더라도 em 반드시 종료
}
emf.close();
// 트랜잭션 단위로 관리되는 Entity Manager는 Tx가 종료되면 close가 되어야하지만, emf는 Application 종료 시, close되어야 함
}
}
|
🖨️발생한 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
Hibernate:
/* select
m.name
from
Team t
join
t.members m */ select
members1_.name as col_0_0_
from
Team team0_
inner join
Member members1_
on team0_.id=members1_.TEAM_ID
|
⭐⭐⭐ 묵시적 조인을 사용하지말고, 명시적 조인 사용 ⭐⭐⭐
'Java > JPA' 카테고리의 다른 글
[JPA_Basic] Fetch Join 2 (0) | 2023.09.28 |
---|---|
[JPA_Basic] Fetch Join (0) | 2023.09.27 |
[JPA_Basic] JPQL 타입 표현과 기타식 (1) | 2023.09.25 |
[JPA_Basic] 조인 (0) | 2023.09.23 |
[JPA_Basic] 페이징 (0) | 2023.09.23 |