티뮤

JPA is not mapped 에러 해결 방법 본문

IT/개발

JPA is not mapped 에러 해결 방법

티뮤 2022. 5. 13. 12:30
728x90

에러내용

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: abandonment is not mapped [SELECT a FROM abandonment a]
	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:757)
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:848)
	at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
	at com.team.petBatch.DBData.getDBData(DBData.java:30)
	at com.team.petBatch.PetBatchApplication.main(PetBatchApplication.java:24)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: abandonment is not mapped [SELECT a FROM abandonment a]
	at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
	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:162)
	at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:636)
	at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:748)
	... 4 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: abandonment is not mapped
	at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:170)
	at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
	at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:77)
	at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:334)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3782)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3671)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:746)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:602)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:339)
	at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:287)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192)
	... 10 more

위와 같이 createQuery 메소드 사용 시 테이블을 찾지 못하는 에러가 발생하는 경우가 있다.

createQuery 메소드에서는 테이블명이 아닌 Entity 클래스명을 찾기 때문에 발생하는 에러

@Entity
@Table(name = "abandonment")
public class AbandonmentVO {
String query = "SELECT a FROM abandonment a";
        try{
            List<AbandonmentVO> resultList = em.createQuery(query, AbandonmentVO.class).getResultList();
            for (AbandonmentVO abandonmentVO : resultList) {
                System.out.println("abandonmentVO = " + abandonmentVO);
            }

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

query에 Entity명이 아닌 테이블 명을 입력해줬기 때문이다.

해결 방법은 

1. query에 Entity 명을 입력해준다.

String query = "SELECT a FROM AbandonmentVO a";

2. Entity명을 테이블명과 같이 바꿔준다.

@Entity(name ="abandonment")
@Table(name = "abandonment")
public class AbandonmentVO {
728x90

'IT > 개발' 카테고리의 다른 글

JPA Unknown entity : [entityname]  (0) 2022.04.12
스프링 부트 프로젝트 실행 에러  (0) 2022.04.11
H2 DB 접속 에러 잡기  (0) 2022.04.05
System.getProperty() 메소드 활용  (0) 2021.02.13
javac 명령어 / 옵션 정리  (0) 2021.02.06
Comments