1

Im trying to configure a simple JPA project and I'm very stuck.

I have a working dataSource (MySQL) & GlassFish with multiple tables.

I did the following steps (all of the files generated by Intellij):

  1. Create new project with Java EE Persistance with Hibernate Provider.

  2. Import DB that has a Student table.

  3. Generate Entity classes with Intellij IDE.

  4. Import Hibernate-entitymanager library from Maven.

  5. Import MySQL connector.

  6. Try to run a simple JPA program to create new row but I get an exeption :

    No Persistence provider for EntityManager named persistenceUnit

What can be the problem? I tried almost every tutorial and every question here but with no success!

Please Help me!

(sometimes the Hibernate in the persistance provider is colored red)

persistance:

    <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

    <persistence-unit name="NewPersistenceUnit">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>modules.CourseEntity</class>
        <class>modules.StuCouEntity</class>
        <class>modules.StudentsEntity</class>
        <class>modules.TeacherEntity</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="123456"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

Main.java:

import modules.StudentsEntity;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class MainClass
{
    public static void main(String[] args)
    {
        StudentsEntity stu = new StudentsEntity();
        stu.setName("David");

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        try
        {
            em.persist(stu);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
            em.getTransaction().rollback();
        }
        finally
        {
            em.close();
        }



    }
}

error:

June 16, 2017 5:47:07 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
June 16, 2017 5:47:07 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.5.Final}
June 16, 2017 5:47:07 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
June 16, 2017 5:47:07 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistenceUnit
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at MainClass.main(MainClass.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
J. Doe
  • 13
  • 1
  • 3

1 Answers1

1

You defined

<persistence-unit name="NewPersistenceUnit">

while tryng to call Persistence.createEntityManagerFactory("persistenceUnit");

change NewPersistenceUnit to persistenceUnit

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28
  • You're totally my hero. If I wasn't married already I would ask you to be my best man. Another question - It's only works with org.hibernate:hibernate-entitymanager:4.1.5.Final and crash with the latest version org.hibernate:hibernate-entitymanager:5.10.Final do you know why? – J. Doe Jun 16 '17 at 15:46
  • Unfortunately I'm married too)) haha. What is the exception when it's crashed? By the way , see here for a possible reason https://stackoverflow.com/questions/39410183/hibernate-5-2-2-no-persistence-provider-for-entitymanager – Mike Adamenko Jun 16 '17 at 15:48