Java/JPA with Error

[해결 방법] org.hibernate.QueryException

HJ0216 2023. 9. 26. 17:34

👉 기본 환경

- Language: Java

- DB: H2 Database

- IDE: IntelliJ

 

 

⌨️ 코드

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는 컬렉션에 대한 직접적인 접근을 허용하지 않음)

 

 

📰 해결 방법

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되어야 함
 
    }
}
 

FROM 절에서 명시적 조인을 통해 별칭을 얻으면 별칭을 통해 탐색