简体   繁体   中英

Error : No persistence provider for EntityManager

i'm trying to start my understanding of Hibernate . But I have a problem. With my researches I found lots of things but nothing to fix my issues.. "org.hibernate.ejb.HibernatePersistence is written in red color.

My persistence.xml

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="demojpa">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.format_sql" value="true"/>

            <!-- Configuration de la BDD -->
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/demojpa" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />

            <!-- Specifie le dialecte SQL utilisé pour communiquer avec la BDD -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.MYSQLDialect"/>

            <!-- Indique a Hibernate de re-creer la BDD au lancement de l'applciation -->
            <property name="hbm2ddl.auto" value="create"/>

        </properties>
    </persistence-unit>
</persistence>

my class

public class DemoJpa {

    public static void main(String[] args) {
        // 1 Ouverture unité de travail JPA
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("demojpa");
        EntityManager em = emf.createEntityManager();
}

To finish a picture of my root... Because I think there is a problem in this one....

我的根

Thanks everyone for your help!

In your persistence.xml replace the deprecated declaration (removed in Hibernate versions 5+)

<provider>org.hibernate.ejb.HibernatePersistence</provider>

with

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

Basically, this should make the "red color" disappear.

Notice well, that Hibernate in version 4.3.x supports JPA 2.1 (see the table Supported JPA Versions ). Important: You should also declare in the head of the persistence.xml the following:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
             version="2.1">

This will give you full JPA 2.1 support and tell the ORM to make use of version 2.1.

As you're just getting started, better start with Hibernate in version 5.2 or above, as these releases will be supported longer than 4.3.x which is already considered "maintenance only" and no longer actively improved or developed for.

For reference, the persistence provider deprecation topic has been discussed earlier.

Hope it helps.

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