Could not find any META-INF/persistence.xml file in the classpath 오류 해결

jimmy·2024년 9월 7일

문제

자바 ORM 표준 JPA 프로그래밍 - 기본편에서 JPA 프로젝트 초기설정을 하는데
main에서 다음과 같은 코드를 실행하는데 오류가 발생했다.
-> Could not find any META-INF/persistence.xml file in the classpath

분석

main

EntityManagerFactory emf = public class JpaMain {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello");
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
    <persistence-unit name="hello">
        <properties>
            <!-- 필수 속성 -->
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>

            <!-- 옵션 -->
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="true"/>
            <!--<property name="hibernate.hbm2ddl.auto" value="create" />-->
        </properties>
    </persistence-unit>
</persistence>

-> 파일이나 폴더 이름도 틀린 거 없고 경로도 잘 맞게 작성했고,

persistence-unit name="hello"

여기서 hello라고 이름도 잘 설정한 걸 볼 수 있다. 그럼 어떻게 해결해야할까?
.
.
이전 캐시가 잘못된 경로 정보를 포함하고 있을 수 있어, 이를 정리함으로써 이러한 오류를 해결할 수 있다고 한다.

캐시 정리

IntelliJ에서는 "File" > "Invalidate Caches / Restart" 로 캐시를 비울 수 있다.

해결

profile
백문이 불여일기

0개의 댓글