How to configure & package a simple Java app to use JPA
Asked Answered
M

2

0

I'm trying to learn JPA and I want create a simple Java command line app that will use JPA to query and update a database table. I mapped out the the simple code needed to do this. But I don't know how to configure the directory structure, where to place the persistence.xml file, packaging and so on. This is just a quick and dirty learning exercise so I want to keep this as simple as possible. Can someone spell out the steps in order to do this?

I'm using Weblogic 10.3.

Marleah answered 12/12, 2009 at 2:46 Comment(2)
You really have to pick one from "simple Java command line app" and "using Weblogic"Ballou
:) OK, my PC has the WL 10.3 libraries. Despite that, I'm trying to keep the app as simple as possible. I'm new to Java/JEE/JPA so I'm a little confused about how to set up the dir structure and other configuration/packaging nuances.Marleah
F
1

persistence.xml goes in the META-INF directory that is at the same level as your persistence classes. Here's an example of some valid and invalid configurations. In the non-Java EE apps I've written, I build the JAR with persistence.xml in WEB-INF/classes/META-INF/, because my JPA classes are in WEB-INF/classes/.

Furlana answered 12/12, 2009 at 2:54 Comment(2)
Does the above apply if I'm not creating a web app, rather a simple command line app?Marleah
I believe it should -- instead of your classes being in WEB-INF/classes/, they'll just be in classes/, so the META-INF should be a top-level directory.Furlana
G
1

Not sure to understand what WebLogic has to do with a Java command line app :)

Anyway, all the details you are looking for are available in the Persistence Units section of The Java EE 5 Tutorial that I'm quoting below:

Persistence Units

A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store.

Persistence units are defined by the persistence.xml configuration file. The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit’s root.

Each persistence unit must be identified with a name that is unique to the persistence unit’s scope.

Persistent units can be packaged as part of a WAR or EJB JAR file, or can be packaged as a JAR file that can then be included in an WAR or EAR file.

If you package the persistent unit as a set of classes in an EJB JAR file, persistence.xml should be put in the EJB JAR’s META-INF directory.

If you package the persistence unit as a set of classes in a WAR file, persistence.xml should be located in the WAR file’s WEB-INF/classes/META-INF directory.

If you package the persistence unit in a JAR file that will be included in a WAR or EAR file, the JAR file should be located:

  • In the WEB-INF/lib directory of a WAR.
  • In the top-level of an EAR file.
  • In the EAR file’s library directory.

The persistence.xml File

persistence.xml defines one or more persistence units. The following is an example persistence.xml file.

<persistence>
    <persistence-unit name="OrderManagement">
        <description>This unit manages orders and customers.
            It does not rely on any vendor-specific features and can
            therefore be deployed to any persistence provider.
        </description>
        <jta-data-source>jdbc/MyOrderDB</jta-data-source>
        <jar-file>MyOrderApp.jar</jar-file>
        <class>com.widgets.Order</class>
        <class>com.widgets.Customer</class>
    </persistence-unit>
</persistence>

This file defines a persistence unit named OrderManagement, which uses a JTA-aware data source jdbc/MyOrderDB. The jar-file and class elements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses. The jar-file element specifies JAR files that are visible to the packaged persistence unit that contain managed persistence classes, while the class element explicitly names managed persistence classes.

The jta-data-source (for JTA-aware data sources) and non-jta-data-source (non-JTA-aware data sources) elements specify the global JNDI name of the data source to be used by the container.

Glaive answered 12/12, 2009 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.