2015-07-21 139 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

相关问题