简体   繁体   中英

Can't find persistence.xml and provider while both specified

When I am running my unit test I these two errors

org.hibernate.jpa.boot.internal.PersistenceXmlParser doResolve INFO: HHH000318: Could not find any META-INF/persistence.xml file in the classpath

and

javax.persistence.PersistenceException: No Persistence provider for EntityManager named dbContext

I don't get them both because as you can see in the image en persistence.xml file below this text everything is there that he is complaining about.

FolderStructure

and here you have my persistance.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
    <persistence-unit name="dbContext">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="{urltodb}"/>
            <property name="hibernate.connection.autocommit" value="false"/>
            <property name="hibernate.connection.username" value="{myusername}"/>
            <property name="hibernate.connection.password" value="{mypassword}"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
            <property name="hibernate.connection.CharSet" value="utf8"/>
            <property name="hibernate.connection.characterEncoding" value="utf8"/>
            <property name="hibernate.connection.useUnicode" value="true"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

Your folder structure is completely mixed up:

  1. Your src -Folder is marked as source-folder (blue), so the subfolder main is interpreted as Java-Package
  2. Your resources-folder is within your sources/main -folder That's why Intellij interprets it as package main.resources.META-INF .
  3. (All your real packages start with upper-case. That's not an error, but unusual nevertheless)

How your folder structure should look like:

  • src/main/java => Mark this as source folder and put all your java packages there
  • src/main/resources => Mark this as resources folder an put your META-INF there

See also: How to create a test directory in Intellij 13?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM