2015-07-21 119 views
1

我正在使用JPA和Hibernate的Swing應用程序。但每次我嘗試調用下面的代碼獲取EntityManagerHibernate,JPA,沒有EntityManager的持久性提供者名爲

try { 
    Class.forName("org.postgresql.Driver"); 
} catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
} 
EntityManagerFactory emf = Persistence.createEntityManagerFactory("abcd"); 
EntityManager em = emf.createEntityManager(); 

我得到以下情況例外:

Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named abcd: Provider named org.hibernate.jpa.HibernatePersistenceProvider threw unexpected exception at create EntityManagerFactory: 
javax.persistence.PersistenceException 
javax.persistence.PersistenceException: Unable to build entity manager factory 

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set 

路徑persistence.xmlmy.jar/meta-inf/persistence.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<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" 
      version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> 
    <persistence-unit name="abcd" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> 
     <properties> 
      <property name="hibernate.connection.url" value="xxx"/> 
      <property name="hibernate.connection.password" value="xxx"/> 
      <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/> 
      <property name="hibernate.connection.user" value="xxx"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

依賴關係:

除了JUnit的所有相關性在類路徑

<dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.8</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.persistence</groupId> 
     <artifactId>persistence-api</artifactId> 
     <version>1.0.2</version> 
     <scope>compile</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>4.3.10.Final</version> 
     <scope>compile</scope> 
    </dependency> 
+0

請檢查類org.hibernate.ejb.Hibernat ePersistence位於您的類路徑中。 – ozgur

回答

0

看來,它不能找到你persistence.xml文件。 persistence.xml文件的正確文件夾是META-INF而不是meta-inf

相關問題