Persistence.xml where to put in eclipse project
Asked Answered
F

3

11

Simple question...
I have a few projects in Eclipse enviroment
MainApp(Enterprise Application Project) which "includes"

  1. WebController (EJB Project)
  2. WebModel (JPA Project)
  3. WebView(Dynamic Web Project)


Problem is where to put persistance.xml file?

Getting exception: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName DataModel

Finely answered 6/6, 2011 at 17:23 Comment(0)
L
13

The JPA 2.0 specification in section 8.2 states the requirements for packaging. It says

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.

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.

However, I am not sure if Eclipse packages it up as required for Web server deployment. I know it does in a EAR project.

Laughry answered 7/6, 2011 at 9:59 Comment(0)
I
14

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.

Incisure answered 6/6, 2011 at 21:51 Comment(0)
L
13

The JPA 2.0 specification in section 8.2 states the requirements for packaging. It says

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.

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.

However, I am not sure if Eclipse packages it up as required for Web server deployment. I know it does in a EAR project.

Laughry answered 7/6, 2011 at 9:59 Comment(0)
A
-1

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.

project structure

Antilogism answered 5/8, 2019 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.