2013-05-08 50 views
2

我有一个标准的Java应用程序(而不是Java EE),并且我正在尝试了解JPA。问题是,当我运行该项目,控制台输出:JPA:EntityManager没有持久化提供程序 - 标准Java应用程序

1218 [main] INFO org.hibernate.ejb.Ejb3Configuration - HHH000318: Could not find any META-INF/persistence.xml file in the classpath 
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named entities 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89) 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60) 
at com.svprdga.test.App.main(App.java:26) 

所以我想它没有找到我的persistence.xml文件,我搜索周围,我看到其中涉及的Java EE文件夹,如Web修复-INF,但这不是我的情况,我也尝试了另一种解决方案,但问题仍然存在。

我在META-INF persistence.xml中,下/ src目录,其内容是:

<?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="entities" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class>com.svprdga.test.database.entity</class> 
    <properties> 

     <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/hibtest"/> 
     <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
     <property name="toplink.jdbc.user" value="root"/> 
     <property name="toplink.jdbc.password" value=""/> 

     <!-- EclipseLink should create the database schema automatically --> 
     <property name="eclipselink.ddl-generation" value="create-tables" /> 
     <property name="eclipselink.ddl-generation.output-mode" 
     value="database" /> 
    </properties> 

    </persistence-unit> 
</persistence> 

感谢您的帮助。

+0

您似乎有一个知道使用哪个JPA实现的问题。在一个地方你选择Hibernate,然后去为TopLink指定很多属性,然后为EclipseLink指定更多属性。我建议你在想更多的时候解决你想使用哪个JPA提供商 – DataNucleus 2013-05-08 13:18:00

+0

当然;问题是,当我尝试使用javax.persistence时,控制台会提示我提供持久性提供程序;但我不知道为了继续使用javax.persistence应该放什么。 – svprdga 2013-05-09 15:52:07

回答

9

我找到了解决方案,如果您使用的是Maven,则看起来META-INF必须为,并且在src/main/resources之下;那么persistence.xml将会被捕获。

+0

是我的解决方案...带了我1小时,谢谢! – 2013-11-27 11:47:42

相关问题