Persistence.xml 放在 eclipse 项目的什么位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6255679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persistence.xml where to put in eclipse project
提问by Marcin Petrów
Simple question...
I have a few projects in Eclipse enviroment
MainApp(Enterprise Application Project) which "includes"
简单的问题...
我在 Eclipse 环境
MainApp(Enterprise Application Project) 中有几个项目,其中“包括”
- WebController (EJB Project)
- WebModel (JPA Project)
- WebView(Dynamic Web Project)
- WebController(EJB 项目)
- WebModel(JPA 项目)
- WebView(动态Web项目)
Problem is where to put persistance.xml file?
问题是把persistance.xml文件放在哪里?
Getting exception: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName DataModel
获取异常:java.lang.IllegalStateException:无法为 unitName DataModel 检索 EntityManagerFactory
回答by Vineet Reynolds
The persistence.xml file is typically present in the src/META-INF
directory of the persistence (JPA) project. If the project was created with Eclipse, it is typically created and placed there by default.
persistence.xml 文件通常存在于src/META-INF
持久性 (JPA) 项目的目录中。如果项目是使用 Eclipse 创建的,那么默认情况下它通常会被创建并放置在那里。
回答by Edwin Dalorzo
The JPA 2.0 specificationin section 8.2 states the requirements for packaging. It says
第 8.2 节中的JPA 2.0 规范说明了包装要求。它说
A persistence unit is defined by a persistence.xml file. The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit.
In Java EE environments, the root of a persistence unit must be one of the following:
- an EJB-JAR file
- the WEB-INF/classes directory of a WAR file[80]
- a jar file in the WEB-INF/lib directory of a WAR file
- a jar file in the EAR library directory
- an application client jar file
It is not required that an EJB-JAR or WAR file containing a persistence unit be packaged in an EAR unless the persistence unit contains persistence classes in addition to those contained within the EJB-JAR or WAR. See Section 8.2.1.6.
持久性单元由persistence.xml 文件定义。META-INF 目录包含persistence.xml 文件的jar 文件或目录称为持久性单元的根。
在 Java EE 环境中,持久性单元的根必须是以下之一:
- 一个 EJB-JAR 文件
- WAR 文件的 WEB-INF/classes 目录 [80]
- WAR文件的WEB-INF/lib目录下的jar文件
- EAR 库目录中的 jar 文件
- 应用程序客户端 jar 文件
不需要将包含持久性单元的 EJB-JAR 或 WAR 文件打包到 EAR 中,除非持久性单元除了包含在 EJB-JAR 或 WAR 中的持久性类之外还包含持久性类。见第 8.2.1.6 节。
You can read the rest of the chapter if you deem it necessary. In Eclipse that means that you need to configure the dependencies between your projects to ensure the deployment files are packaged appropriately. You typically configure that in Project->Properties->Build Path in the Projects tab.
如果您认为有必要,您可以阅读本章的其余部分。在 Eclipse 中,这意味着您需要配置项目之间的依赖关系,以确保正确打包部署文件。您通常在“项目”选项卡中的“项目”->“属性”->“构建路径”中进行配置。
However, I am not sure if Eclipse packages it up as required for Web server deployment. I know it does in a EAR project.
但是,我不确定 Eclipse 是否按照 Web 服务器部署的需要对其进行了打包。我知道在 EAR 项目中确实如此。
回答by DarkGourab
For JPA project or even for normal Java project, you just need to create META-INF folder under the src and add the persistence.xml there. That's all. now at the time of application loading in the jvm it will automatically be fetched.
对于 JPA 项目,甚至对于普通的 Java 项目,您只需要在 src 下创建 META-INF 文件夹并在那里添加persistence.xml。就这样。现在在 jvm 中加载应用程序时,它将自动获取。