找不到Hibernate JPA项目的persistence.xml

时间:2015-05-26 15:34:00

标签: java eclipse hibernate jpa

您好我是Hibernate JPA的新手。我正在使用eclipse kepler 4.3.2并创建一个简单的java项目。我没有使用maven来编译项目。我在项目的

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" >
    <persistence-unit name="rawsjpa" transaction-type="RESOURCE_LOCAL">
        <!-- <class>com.mumz.test.hibernatesearch.entitybeans.MHSBookEntityBean</class>
        <class>com.mumz.test.hibernatesearch.entitybeans.MHSBookShelfEntityBean</class> -->
        <properties>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
            <property name="hibernate.connection.password" value="Test2@cdn"/>
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=rschcdndb1d.nam.nsroot.net)(PORT=1526))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=OSRDD1)))"/>
            <property name="hibernate.connection.username" value="AW"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle8iDialect"/>
        </properties>
    </persistence-unit>
</persistence>

我正在尝试创建EntityManager的以下示例主程序。

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

/**
 * 
 */

/**
 * @author jp48346
 *
 */
public class TestRawsConnection {

    public static void main(String args[]){
        EntityManager entityManager = Persistence.createEntityManagerFactory("rawsjpa").createEntityManager();
        if(entityManager!=null){
            System.out.println("************* EntityManager is obtained *****************");
        }
    }

}

下面是eclipse项目.classpath文件

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry excluding="main/resources/META-INF/" kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
    <classpathentry kind="lib" path="C:/Users/jp48346/.m2/repository/org/hibernate/hibernate-entitymanager/4.2.17.Final/hibernate-entitymanager-4.2.17.Final.jar"/>
    <classpathentry kind="lib" path="C:/Users/jp48346/.m2/repository/org/hibernate/common/hibernate-commons-annotations/4.0.2.Final/hibernate-commons-annotations-4.0.2.Final.jar"/>
    <classpathentry kind="lib" path="C:/Users/jp48346/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
    <classpathentry kind="lib" path="C:/Users/jp48346/.m2/repository/org/hibernate/hibernate-core/4.2.17.Final/hibernate-core-4.2.17.Final.jar"/>
    <classpathentry kind="lib" path="C:/Users/jp48346/.m2/repository/org/jboss/logging/jboss-logging/3.1.0.GA/jboss-logging-3.1.0.GA.jar"/>
    <classpathentry kind="lib" path="C:/Users/jp48346/.m2/repository/javax/transaction/jta/1.1/jta-1.1.jar"/>
    <classpathentry kind="lib" path="C:/Users/jp48346/.m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

当我尝试执行程序时,我收到以下异常

May 26, 2015 8:52:17 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
May 26, 2015 8:52:18 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.17.Final}
May 26, 2015 8:52:18 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 26, 2015 8:52:18 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
May 26, 2015 8:52:18 PM org.hibernate.ejb.Ejb3Configuration configure
INFO: 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 rawsjpa
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at TestRawsConnection.main(TestRawsConnection.java:15)

有人可以告诉我应该在哪里放置persistence.xml来解决上述问题吗?

4 个答案:

答案 0 :(得分:5)

persistence.xml文件应位于名为META-INF的文件夹中。如果您使用的是Maven,请将其放在src/main/resources/META-INF中(如果文件夹不存在则创建该文件夹。)

答案 1 :(得分:1)

您应该使用IDE选项创建一个持久性单元,它将被放置在默认位置。 如果您在IDE中没有或没有找到该选项,它通常位于proyect根文件夹中的META-INF文件夹中,如下所示:  enter image description here

答案 2 :(得分:0)

persistence.xml保留在META-INF文件夹下。

在Eclipse中,它将在

之下
Project --> WebContent --> META-INF 

答案 3 :(得分:0)

如果您正在使用jboss应用程序服务器,那么请确保oracle驱动程序存在于org.hibernate模块下并且在module.xml文件中具有该条目。

此外,右键单击项目并将项目构面设置为JPA,它会自动将您的persitence.xml文件添加到META-INF类应用程序路径中。

请分享您正在使用的应用服务器以及用于连接的oracle驱动程序的更多信息。

不必使用maven。

欢呼声。