2

I'm using persistense api to connect to database

this is pom.xml dependendcies

 <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.8.Final</version>
  </dependency>
  <dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
  </dependency>
  <dependency>
    <groupId>javax.persistence</groupId>
    <artifactId>persistence-api</artifactId>
    <version>1.0.2</version>

persistense.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_1_0.xsd"
    version="1.0">
    <persistence-unit name="firstOne">
         <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.ejb.cfgfile" value="/META-INF/hibernate.cfg.xml" />
        </properties>
    </persistence-unit>
</persistence>

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  <hibernate-configuration>
<session-factory>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">tauren993</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernate</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="hibernate.generate_statistics">false</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hibernate.cache.use_structured_entries">false</property>
    <property name="hibernate.cache.use_second_level_cache">false</property>

    <mapping class="Employee" />
</session-factory>
</hibernate-configuration>

hibernate.cfg.xml and persistense.xml are stored project/META-INF folder and I'm getting error on

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

Stack Trace:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named firstOne
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at Main.main(Main.java:14)
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
luka
  • 101
  • 2
  • 6
  • Please provide the full stack trace of the error. – David Dec 17 '13 at 12:35
  • javax.persistence.PersistenceException: No Persistence provider for EntityManager named firstOne at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) at Main.main(Main.java:14) – luka Dec 17 '13 at 12:40

2 Answers2

1

In your persistance-unit "firstOne" you should include the provider. say like below:

<persistence-unit name="firstOne">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
thiyaga
  • 251
  • 1
  • 7
  • included but the same problem – luka Dec 17 '13 at 12:44
  • Can you update your question with the modified persistance.xml? – thiyaga Dec 17 '13 at 12:45
  • I Update it as you said – luka Dec 17 '13 at 12:47
  • Can you check this url http://stackoverflow.com/questions/3739387/javax-persistence-persistenceexception-no-persistence-provider-for-entitymanage?rq=1 and http://stackoverflow.com/questions/1158159/no-persistence-provider-for-entitymanager-named and see if your problem is related to this? – thiyaga Dec 17 '13 at 12:54
0

You probably miss the entities declaration. If you're in JAVA EE context ensure that you have some classes annotated as entities in your project. Otherwise, in a JAVA SE context you must define the provider as thiyaga said (see the doc) and specify the entities as below :

<persistence-unit name="OrderManagement">
    [...]
    <jar-file>MyOrderApp.jar</jar-file>
     or / and 
    <class>com.widgets.Order</class>
    <class>com.widgets.Customer</class>
</persistence-unit>

Moreover,

You should use JPA 2 if you can

You doesn't need anymore the hibernate.cfg.xml file, just put directly your properties in the <properties> element of the persitence.xmlfile, ensure that the hibernate properties are well prefixed with hibernate.

Gab
  • 7,869
  • 4
  • 37
  • 68
  • You should see an error in logs on server startup during the persistence context initialization – Gab Dec 17 '13 at 12:56
  • In SE context you can add `hibernate.archive.autodetection`property as described by Pascal Thivent here http://stackoverflow.com/questions/1780341/do-i-need-class-elements-in-persistence-xml – Gab Dec 17 '13 at 13:15