如何配置&打包一个简单的 Java 应用程序以使用 JPA

发布于 2024-08-14 23:08:05 字数 193 浏览 2 评论 0 原文

我正在尝试学习 JPA,我想创建一个简单的 Java 命令行应用程序,它将使用 JPA 来查询和更新数据库表。我列出了执行此操作所需的简单代码。但我不知道如何配置目录结构、将persistence.xml文件放在哪里、打包等等。这只是一个快速而肮脏的学习练习,所以我想让它尽可能简单。有人可以阐明执行此操作的步骤吗?

我使用的是Weblogic 10.3。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌酱 2024-08-21 23:08:05

persistence.xml 位于与持久性类处于同一级别的 META-INF 目录中。 这是一个示例一些有效和无效的配置。在我编写的非 Java EE 应用程序中,我在 WEB-INF/classes/META-INF/ 中使用 persistence.xml 构建 JAR,因为我的 JPA 类位于 WEB- INF/类/。

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/.

音盲 2024-08-21 23:08:05

不确定是否了解 WebLogic 与 Java 命令行应用程序有什么关系:)

不管怎样,所有您正在寻找的详细信息都可以在 Java EE 5 教程,我在下面引用:

持久性单元

持久化单元定义了一组
由以下对象管理的所有实体类
EntityManager 实例
应用。这组实体
类代表包含的数据
在单个数据存储中。

持久性单位由以下定义
persistence.xml 配置文件。
JAR 文件或目录
META-INF 目录包含
persistence.xml 称为根
的持久性单元。范围
持久性单位由下式确定
持久性单元的根。

每个持久性单元必须是
用唯一的名称标识
到持久性单元的范围。

持久单元可以打包为
WAR 或 EJB JAR 文件的一部分,或者可以
打包为 JAR 文件,可以
然后包含在 WAR 或 EAR 中
文件。

如果将持久单元打包为
EJB JAR 文件中的一组类,
persistence.xml 应该放在
EJB JAR 的 META-INF 目录。

如果将持久性单元打包为
WAR 文件中的一组类,
persistence.xml 应位于
WAR 文件的
WEB-INF/classes/META-INF目录。

如果将持久性单元打包在
将包含在
WAR 或 EAR 文件,JAR 文件应该
位于:

  • 位于 WAR 的 WEB-INF/lib 目录中。
  • 位于 EAR 文件的顶层。
  • 位于 EAR 文件的库目录中。

persistence.xml 文件

persistence.xml 定义一个或多个
持久性单位。下面是一个
示例 persistence.xml 文件。

<持久化>
    
        <描述>该单元管理订单和客户。
            它不依赖于任何特定于供应商的功能,并且可以
            因此可以部署到任何持久性提供者。
        
        jdbc/MyOrderDB
        MyOrderApp.jar
        com.widgets.Order;
        com.widgets.Customer
    

该文件定义了一个持久化单元
名为 OrderManagement,它使用
JTA 感知数据源
jdbc/MyOrderDBjar 文件
class 元素指定托管
持久化类:实体类,
可嵌入类和映射
超类。 jar-file 元素
指定可见的 JAR 文件
到打包的持久性单元
包含托管持久性类,
而类元素明确地
命名托管持久性类。

jta-data-source(用于 JTA 感知
数据源)和
non-jta-data-source(非 JTA 感知
数据源)元素指定
数据源的全局 JNDI 名称
被容器使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文