Nessun provider di persistenza per" EntityManager "denominato" firstOne " Java


Sto usando l'api persistense per connettermi al database

Questo è pom.dipendenze xml

 <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>

Persistente.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>

Ibernazione.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>

Ibernazione.cfg.xml e persistenza.xml sono memorizzati nella cartella project / META-INF e ricevo un errore su

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

Traccia dello stack:

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)
Author: Angelo Fuchs, 2013-12-17

2 answers

Nella tua unità di persistenza "firstOne" dovresti includere il provider. dì come di seguito:

<persistence-unit name="firstOne">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
 1
Author: thiyaga, 2013-12-17 12:42:07

Probabilmente ti manca la dichiarazione delle entità. Se sei in contesto JAVA EE assicurati di avere alcune classi annotate come entità nel tuo progetto. Altrimenti, in un contesto JAVA SE è necessario definire il provider come detto da thiyaga (vedere il doc ) e specificare le entità come di seguito:

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

Inoltre,

Dovresti usare JPA 2 se puoi

Non hai più bisogno del file hibernate.cfg.xml, basta inserire direttamente le tue proprietà nell'elemento <properties> del file persitence.xml, assicurati che le proprietà hibernate sono ben precedute da hibernate.

 0
Author: Gab, 2013-12-17 12:53:59