简体   繁体   English

在Hibernate JPA项目中找不到persistence.xml

[英]persistence.xml is not found for Hibernate JPA project

Hi I am new to Hibernate JPA. 嗨,我是Hibernate JPA的新手。 I am using eclipse kepler 4.3.2 and create a simple java project. 我正在使用eclipse kepler 4.3.2并创建一个简单的Java项目。 I am not using maven to compile the project. 我没有使用Maven来编译项目。 I am having the below persistence.xml located in the src folder of the project 我在项目的src文件夹中有以下persistence.xml

<?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>

I am having the below sample main program trying to create the EntityManager. 我有下面的示例主程序尝试创建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 *****************");
        }
    }

}

Below is the eclipse project .classpath file 以下是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>

when I am trying to execute the program I am getting the below exception 当我尝试执行程序时,出现以下异常

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)

Can anybody please tell me where should I place persistence.xml to resolve the above issue? 谁能告诉我应该在哪里放置persistence.xml来解决上述问题?

the persistence.xml file should reside in a folder named META-INF . persistence.xml文件应驻留在名为META-INF的文件夹中。 If you are using using Maven, put it in src/main/resources/META-INF (create the folder if it's not there). 如果使用的是Maven,请将其放在src/main/resources/META-INF (如果不存在则创建文件夹)。

You should use your IDE option for create a Persistence Unit, and it will be placed on the default location. 您应该使用IDE选项创建持久性单元,并将其放置在默认位置。 If you don't have or don't found that option in your IDE, it's usually on the META-INF folder located in the proyect root folder, like this: 如果您在IDE中没有该选项或找不到该选项,则通常在proyect根文件夹中的META-INF文件夹中,如下所示: 在此处输入图片说明

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

In Eclipse, it would be under 在Eclipse中,

Project --> WebContent --> META-INF 

If you are using jboss application server then please make sure the oracle driver exist under the org.hibernate module and having the entry in module.xml file. 如果您使用的是jboss应用程序服务器,请确保org.hibernate模块下存在oracle驱动程序,并且在module.xml文件中具有该条目。

Also, right click on project and set project facet to JPA, it will automatically added your persitence.xml file to META-INF class path of application. 另外,右键单击项目并将项目构面设置为JPA,它将自动将persitence.xml文件添加到应用程序的META-INF类路径。

Please share the more information which application server you are using and which oracle driver you are using for connectivity. 请共享更多信息,您正在使用哪个应用程序服务器以及正在使用哪个oracle驱动程序进行连接。

Not compulsory to use maven. 不强制使用Maven。

cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM