Hibernate – javax.persistence.PersistenceException: No Persistence provider for EntityManager named sample

entitymanagerhibernatejpamaven

I am using Hibernate Entity Manager. It complains that it cant find persistence provider but still logs it in the logs :

    TRACE 2011-02-21 22:17:52,843 [main](Ejb3Configuration.java:321) org.hibernate.ejb.Ejb3Configuration - PersistenceMetadata(version=2.0) [
        name: sample
        jtaDataSource: null
        nonJtaDataSource: null
        transactionType: RESOURCE_LOCAL
        provider: org.hibernate.ejb.HibernatePeristence
        useQuotedIdentifiers: false
        classes[
        ]
        packages[
        ]
        mappingFiles[
        ]
        jarFiles[
        ]
        hbmfiles: 0
        properties[
            hibernate.connection.username: scott
            hibernate.connection.password: tiger
            hibernate.dialect: org.hibernate.dialect.OracleDialect
            hibernate.show_sql: true
            hibernate.connection.url: jdbc:oracle:thin:@localhost:1521:krsna
            hibernate.archive.autodetection: class, hbm
            hibernate.connection.driver_class: oracle.jdbc.driver.OracleDriver
        ]]
    Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named sample
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
        at EMPersistDemo2.main(EMPersistDemo2.java:26)

It is evident that it finds it, logs it …but still throws that exception.
Am I missing some thing ?

I am using maven and I placed persistence.xml in src/main/resources/META-INF.
Its contents are :

    <!-- persistence.xml -->
    <persistence ...>
       <persistence-unit name="sample">
        <provider>org.hibernate.ejb.HibernatePeristence</provider>
        <!-- Scan for annotated classes and Hibernate mapping XML files -->
        <properties>
           <property name="hibernate.archive.autodetection" value="class, hbm"/>
           <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
           <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:krsna"/>
           <property name="hibernate.connection.username" value="scott"/>
           <property name="hibernate.connection.password" value="tiger"/>
           <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
           <property name="hibernate.show_sql" value="true"/>
        </properties>
        </persistence-unit>

    </persistence>

org.hibernate.Ejb3Configuration is able to find it javax.persitence.Persistence is not able to find it:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("sample");

Best Answer

I have also faced same issue but I got a solution that : missing META-INF/services/javax.persistence.spi.PersistenceProvider file. for hibernate, it's located in hibernate-entitymanager-XXX.jar, so, if hibernate-entitymanager-XXX.jar is not in your classpath, you will got this error too.