2013-12-17 70 views
2

我使用persistense API連接到數據庫沒有持久性提供 「EntityManager的」 命名 「firstOne的」 Java

這是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和persistense.xml存儲項目/ META-INF文件夾,然後我得到的

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

堆棧跟蹤錯誤:

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) 
+0

請提供錯誤的完整堆棧跟蹤。 – david99world

+0

javax.persistence.PersistenceException:在javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)爲EntityManager的不持久性提供者在javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)命名firstOne \t \t \t在Main.main(Main.java:14) – luka

回答

1

在您的持久性單元「firstOne 「你應該包括提供者。如下所示:

<persistence-unit name="firstOne"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
+0

包括但同樣的問題 – luka

+0

你可以用修改的persistance.xml更新你的問題嗎? – thiyaga

+0

我像你說的那樣更新它 – luka

0

您可能會錯過實體聲明。如果您處於JAVA EE環境中,請確保您的項目中有一些註釋爲實體的類。如果你能

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

此外,

你應該使用JPA 2:否則,在Java SE環境下,你必須定義提供商爲thiyaga說(見doc)和下面指定的實體

您不需要hibernate.cfg.xml文件,只需將您的屬性直接放入persitence.xml文件的<properties>元素中,確保休眠屬性的前綴爲hibernate

+0

在持久化上下文初始化期間,您應該在服務器啓動過程中看到日誌中的錯誤 – Gab

+0

在SE上下文中,您可以添加'hibernate.archive.autodetection'屬性,如Pascal Thivent所述http://stackoverflow.com/questions/1780341/do -i-需要一流的元素,在持久性的XML – Gab

相關問題