2015-04-23 81 views
1

我試圖運行基於JPA的示例項目。我始終得到標題中的錯誤。沒有EntityManager的持久性提供者命名(休眠)

我看這裏: No Persistence provider for EntityManager named

但沒有發現有效的解決方案出現。

這裏是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>~path.example</groupId> 
    <artifactId>example</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <dependencies> 

     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.6</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>ejb3-persistence</artifactId> 
      <version>3.3.2.Beta1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.3.9.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.jst.server.generic</groupId> 
      <artifactId>oc4j</artifactId> 
      <version>1.5.105-v200709061325</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.0.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>4.2.0.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.common</groupId> 
      <artifactId>hibernate-commons-annotations</artifactId> 
      <version>4.0.1.Final</version> 
      <classifier>tests</classifier> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.0-api</artifactId> 
      <version>1.0.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.validation</groupId> 
      <artifactId>validation-api</artifactId> 
      <version>1.0.0.GA</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.6.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.jboss.logging</groupId> 
      <artifactId>jboss-logging</artifactId> 
      <version>3.1.0.CR2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>1.6.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.jboss.spec</groupId> 
      <artifactId>jboss-javaee-6.0</artifactId> 
      <version>1.0.0.Final</version> 
      <type>provided</type> 
      <scope>pom</scope> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.jboss.arquillian.junit</groupId> 
      <artifactId>arquillian-junit-container</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.jboss.spec</groupId> 
      <artifactId>jboss-javaee-6.0</artifactId> 
      <version>1.0.0.Final</version> 
      <type>pom</type> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.jboss.arquillian.container</groupId> 
      <artifactId>arquillian-weld-ee-embedded-1.1</artifactId> 
      <version>1.0.0.CR3</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.jboss.weld</groupId> 
      <artifactId>weld-core</artifactId> 
      <version>1.1.5.Final</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-simple</artifactId> 
      <version>1.6.4</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.jboss.arquillian</groupId> 
       <artifactId>arquillian-bom</artifactId> 
       <version>1.1.8.Final</version> 
       <scope>import</scope> 
       <type>pom</type> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

</project> 

這可能是我有一些依賴,我不需要。由於我沒有經驗,我必須誠實地承認,我非常絕望並且尋找任何可能的答案,並且在那個過程中我添加了一些根本不可用的依賴關係。

這是應該做的EntityManager

package ~path.examples.service; 

import ~path.examples.testjpa.domain.Person; 

import javax.persistence.*; 

/** 
* Created by ME on 2015-04-22. 
*/ 
public class JpaTest { 
    public static void main(String args[]) { 
     EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpaTest"); 
     EntityManager em = entityManagerFactory.createEntityManager(); 
     EntityTransaction userTransaction = em.getTransaction(); 

     userTransaction.begin(); 
     Person person = new Person(); 
     person.setFirstName("Charles"); 
     person.setSurname("Dickens"); 
     em.persist(person); 
     userTransaction.commit(); 
     em.close(); 
     entityManagerFactory.close(); 
    } 
} 

而且類:

  1. Im相當肯定,所有的文件都在合適的位置,我也有一些問題,我,但我似乎已經消除了這個問題(其他錯誤比以前彈出:) :)

  2. 這可能是顯而易見的,我錯過了,在此先感謝f或任何幫助!

編輯:這很可能是問題在於persistance.xml文件,所以在這裏(謝謝指出它)。該文件的位置爲src /主/資源/ META-INF

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" 
      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_2_0.xsd"> 
    <persistence-unit name="JpaTest" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <class>~path.examples.testjpa.domain.Person</class> 
     <properties> 
      <property name="hibernate.show_sql" value="true" /> 
      <property name="hibernate.format_sql" value="true" /> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /> 
      <property name="hibernate.hbm2ddl.auto" value="update" /> 
     </properties> 

    </persistence-unit> 
</persistence> 
+1

你有一個persistence.xml文件嗎? – Vyncent

+0

並假設是:在哪裏? 「我很確定」不是隱藏細節的理由,因爲您犯了一個錯誤的選項仍然非常多。你自己這麼說。 – Gimby

+0

是的,當然我有,我打算髮布它。編輯,謝謝指出,只是忘了:) 順便說一下,位置是正確的幾乎肯定(我必須承認我試驗了一點,在任何其他地方它甚至不會被檢測到),但它很可能是文件內有一個錯誤,我沒有看到。 –

回答

0

你需要改變:

<provider>org.hibernate.ejb.HibernatePersistence</provider> 

到:

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
+1

我很抱歉地說,但顯然你錯了。我只通過改變 <持久性單元名稱=「JpaTest」交易類型=「RESOURCE_LOCAL」> 到 <持久性單元名稱=「jpaTest」交易類型=「RESOURCE_LOCAL」>消除了誤差((顯然簡單拼寫錯誤)) 其中刪除了以前的錯誤,但發了新錯誤 「應用程序必須提供JDBC連接」 在行 userTransaction。開始(); 當我試着做你說的,它讓我退後一步到以前的錯誤:) 謝謝你嘗試,但我現在將欣賞幫助新的 –

+0

是嗎? [這個怎麼樣](http://stackoverflow.com/questions/22691252/error-deprecated-persistenceprovider-use-hibernatepersistenceprovider-teady)? –

+0

我遠非試圖貶低你的幫助,因爲這是非常受歡迎的,顯然你對這個話題的瞭解比我多得多。我只是在說會發生什麼。做你的建議讓我退後一步,糾正錯誤,我通過糾正拼寫錯誤。 –

1

也許你沒有添加休眠罐子到項目類路徑?缺少提供程序jar(在本例中爲Hibernate)不會顯示編譯錯誤,因爲它們在運行時期間是必需的。

+1

我正在使用maven,並添加了依賴關係。現在我剛剛證實所有的罐子都在它的位置上。 –

相關問題