了解 JPA 中的 Persistence.xml

发布于 2024-12-09 15:17:08 字数 255 浏览 1 评论 0原文

我试图了解以下事情:

当我创建一个 EJB 项目并将其部署到 Glassfish 时,我是否在 Glassfish 的管理员中心设置 JDBC 资源/连接池,或者我是否在 Glassfish 中添加用户名、密码等的所有不同属性持久性.xml?我一点也不明白。

我也不明白为什么我们同时拥有 JDBC 资源和 JDBC 连接池。它是什么以及它们之间有什么区别?有人可以向我解释这些事情或/并提供一些有关 persistence.xml 文件及其周围部分的良好链接吗?

I am trying to understand the following things:

When I make an EJB project and deploys it to Glassfish do I set up JDBC resources/connection pools at the administrator center of Glassfish or do I add all the different properites for username, password etc in the persistence.xml? I don't understand one bit of that.

I do not understand why we have both JDBC resource and JDBC Connection pool either. What is it and what is the difference between them? Could somebody explain me these things or/and provide some good link about the persistence.xml file and the part around it?

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

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

发布评论

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

评论(3

与他有关 2024-12-16 15:17:08

最好定义 JDBC 资源,而不是将信息放在 persistence.xml 中。通过这种方式,您将利用连接池。您将在 persistence.xml 中定义为 JDBC 资源提供的 JNDI 名称。

它是什么以及它们之间有什么区别

下面我粘贴了 Glassfish 3.x 帮助的一些部分。一探究竟。这确实很有帮助

JDBC 连接池

JDBC 连接池包含一组 JDBC 连接,这些连接是在注册连接池时创建的

JDBC 资源

Java 数据库连接 (JDBC) 资源(数据源)为应用程序提供连接到数据库的方法。通常,管理员为域中部署的应用程序访问的每个数据库创建一个 JDBC 资源;但是,可以为一个数据库创建多个 JDBC 资源。

应用程序通过在 Java 命名和目录接口 (JNDI) API 树上查找数据源,然后请求连接,从连接池获取数据库连接。与数据源关联的连接池提供与应用程序的连接。

将数据源(JDBC)资源视为某种类型的数据服务(连接池)外观的工厂。在这种情况下,它隐式地从池中获取连接并将其提供给您的应用程序。

persistence.xml 示例:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="WebApplication2PU" transaction-type="JTA">
    <jta-data-source>jdbc/sample</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

这一行是我为 JDBC 资源指定的 JNDI 名称:

 <jta-data-source>jdbc/sample</jta-data-source>

您不需要在 persistence.xml 中以这种方式定义与数据库连接相关的任何内容...只需资源的 JNDI 名称

It's better to define a JDBC resource rather than putting the information in the persistence.xml. In this way you are going to take advantage of connection pooling. You are going to define the JNDI name you provided for the JDBC resource in the persistence.xml.

What is it and what is the difference between them

Below I pasted some parts of the Glassfish 3.x help. Check it out. It's really helpful

JDBC connection pool

A JDBC connection pool contains a group of JDBC connections that are created when the connection pool is registered

JDBC resource

A Java DataBase Connectivity (JDBC) resource (data source) provides applications with the means of connecting to a database. Typically, the administrator creates a JDBC resource for each database accessed by the applications deployed in a domain; however, more than one JDBC resource can be created for a database.

Applications get a database connection from a connection pool by looking up a data source on the Java Naming and Directory Interface (JNDI) API tree and then requesting a connection. The connection pool associated with the data source provides the connection to the application.

Think of the data source(JDBC) resource as a factory for a facade of some type of data service(Connection Pool). In this case it implicitly gets a connection from the pool and provides it to your application.

An example persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="WebApplication2PU" transaction-type="JTA">
    <jta-data-source>jdbc/sample</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

This line is the JNDI name I gave to my JDBC resourse:

 <jta-data-source>jdbc/sample</jta-data-source>

You dont need to define anything related to the database connection in your persistence.xml this way...just the JNDI name of the resource

手长情犹 2024-12-16 15:17:08

当您在应用程序服务器中配置数据源时,您只需在 persistence.xml 中设置该数据源的 JNDI 名称即可。

When you configure a data source in your application server, all you need to set in persistence.xml is the JNDI name of that data source.

余生再见 2024-12-16 15:17:08

我在学习 Spring 和 Hibernate 的书中发现了这一点。这本书的名字是Professional Java for Web Applications,作者是Nicholas S. Williams。我认为这对很多人都会有帮助。

创建持久性配置:

要使用您创建的实体,您必须定义持久性单元。这样做很简单。创建一个
persistence.xml 文件与部署描述符没有什么不同,但选项要少得多
担心。持久性配置文件的根元素是。这
元素可以包含一个或多个元素。没有其他元素
内。 有两个属性: name 指定的名称
持久化单元和事务类型表示该持久化单元是否使用Java
事务 API (JTA) 事务或标准本地事务。
您必须指定一个名称,这是您在代码中定位持久性单元的方式。如果没有指定,
transaction-type 在 Java EE 应用程序服务器中默认为 JTA,在 Java 中默认为 RESOURCE_LOCAL
SE环境或者简单的Servlet容器。但是,为了防止意外行为,最好
始终显式设置此值,而不是依赖默认值。
包含以下内部元素。它们都不是必需的(所以
可以为空);但是,您必须指定在
以下顺序:

  • 包含此持久单元的有用描述。虽然这使得
    读取持久性文件更容易,它没有语义价值。
  • 指定 javax.persistence.spi 的完全限定类名
    用于此持久性单元的.PersistenceProvider 实现。默认情况下,当您
    查找持久性单元,API 将使用类路径上的第一个 JPA 提供程序。你
    可以包含此元素来强制指定特定的 JPA 提供程序。

  • 您可以使用 (但不能同时使用两者)来
    使用 JNDI 数据源资源。仅在以下情况下才可以使用
    事务类型为JTA;同样,您只能使用
    如果事务类型是 RESOURCE_LOCAL。指定数据源会导致持久化
    单元使用该数据源进行所有实体操作。

  • 指定 XML 映射文件的类路径相对路径。如果你不这样做
    指定任何 ,提供程序会查找 orm.xml。您可以指定多个
    元素使用多个映射文件。

  • 您可以使用一个或多个 元素来指定一个或多个 JAR 文件
    JPA 提供程序应扫描映射注释的实体。任何@Entity、@Embeddable、
    @javax.persistence.MappedSuperclass 或 @javax.persistence.Converter 类
    找到的内容添加到持久化单元中。

  • 您可以使用一个或多个元素来指示特定的@Entity、@Embeddable、
    应添加到持久性单元的 @MappedSuperclass 或 @Converter 类。
    您必须使用 JPA 注释来注释一个或多个类。

  • 使用 true 表示提供者应该忽略类而不是
    使用 指定。省略 或使用
    false 导致 JPA
    提供程序扫描持久性文件的类路径位置以查找带 JPA 注释的类。如果
    persistence.xml 位于 JAR 文件中,会扫描该 JAR 文件(并且仅扫描该 JAR 文件)
    用于课程。如果 persistence.xml 位于基于目录的类路径位置(例如 /
    WEB-INF/classes),该目录(并且仅该目录)会被扫描以查找类。之前
    Hibernate 4.3.0 和 Spring Framework 3.2.5,将此元素指定为 false 值
    被错误地解释为真实。

  • 指示如何在持久化单元中缓存实体(如果 JPA
    提供者支持缓存,这是可选的)。 NONE 禁用缓存,而 ALL 启用
    缓存所有实体。 ENABLE_SELECTIVE 表示只有@javax 注解的实体
    .persistence.Cacheable 或 @Cacheable(true) (或在 orm.xml 中标记为可缓存)
    被缓存。 DISABLE_SELECTIVE 会导致缓存除带注释的实体之外的所有实体
    @Cacheable(false) (或在 orm.xml 中标记为不可缓存)。默认值,
    UNSPECIFIED,意味着 JPA 提供者决定有效的默认值是什么。休眠
    ORM 默认为 ENABLE_SELECTIVE,但依赖于此是不可移植的。

  • 指示是否以及如何将 Bean 验证应用于实体。
    NONE 表示未启用 Bean Validation,而 CALLBACK 则使提供者
    验证插入、更新和删除时的所有实体。 AUTO 的有效值为 CALLBACK
    如果类路径上存在 Bean Validation 提供程序,则有效值为 NONE(如果不存在)
    Bean 验证提供程序存在于类路径中。如果启用验证,JPA 提供程序
    配置一个新的验证器来验证您的实体。如果您配置了特殊的
    带有自定义本地化错误代码的 Spring Framework Validator、JPA 提供程序
    忽略它。因此,最好将验证模式设置为 NONE 并使用 Bean Validation
    在调用持久层之前。

  • 提供了一种指定其他 JPA 属性的方法,包括标准 JPA
    属性(例如 JDBC 连接字符串、用户名和密码,或架构生成
    设置)以及特定于提供程序的属性(例如 Hibernate 设置)。您指定
    使用嵌套元素的一个或多个属性,每个属性都有一个名称和值
    属性。

Nicholas S. Williams,Web 应用程序的专业 Java,(印第安纳州印第安纳波利斯:John Wiley & Sons, Inc.,2014 年),第 584-585 页

I found this in the book that I read to learn Spring and Hibernate. The book name is Professional Java for Web Applications written by Nicholas S. Williams. I think this will be helpful for many people.

Creating the Persistence Configuration:

To use the entities you create, you must define a persistence unit. Doing so is simple. Create a
persistence.xml file not dissimilar from a deployment descriptor, but with far fewer options
to worry about. The root element of a persistence configuration file is <persistence>. This
element may contain one or more <persistence-unit> elements. No other elements are
within <persistence>. <persistence-unit> has two attributes: name specifies the name of
the persistence unit and transaction-type indicates whether this persistence unit uses Java
Transaction API (JTA) transactions or standard local transactions.
You must specify a name, which is how you locate the persistence unit in code. If not specified,
transaction-type defaults to JTA in a Java EE application server and RESOURCE_LOCAL in a Java
SE environment or simple Servlet container. However, to prevent unexpected behavior it’s best to
always set this value explicitly instead of relying on a default value.
<persistence-unit> contains the following inner elements. None of them are required (so
<persistence-unit> may be empty); however, you must specify whichever elements you use in the
following order:

  • <description> contains a useful description for this persistence unit. Although it makes
    reading the persistence file easier, it has no semantic value.
  • <provider> specifies the fully qualified class name of the javax.persistence.spi
    .PersistenceProvider implementation used for this persistence unit. By default, when you
    look up the persistence unit, the API will use the first JPA provider on the classpath. You
    can include this element to mandate a specific JPA provider.

  • You can use either <jta-data-source> or <non-jta-data-source> (but not both) to
    use a JNDI DataSource resource. You may use <jta-data-source> only if
    transaction-type is JTA; likewise you may use <non-jta-data-source> only
    if transaction-type is RESOURCE_LOCAL. Specifying a DataSource causes the persistence
    unit to use that DataSource for all entity operations.

  • <mapping-file> specifies the classpath-relative path to an XML mapping file. If you don’t
    specify any <mapping-file>, the provider looks for orm.xml. You may specify multiple
    <mapping-file> elements to use multiple mapping files.

  • You can use one or more <jar-file> elements to specify a JAR file or JAR files that the
    JPA provider should scan for mapping-annotated entities. Any @Entity, @Embeddable,
    @javax.persistence.MappedSuperclass, or @javax.persistence.Converter classes
    found are added to the persistence unit.

  • You can use one or more <class> elements to indicate specific @Entity, @Embeddable,
    @MappedSuperclass, or @Converter classes that should be added to the persistence unit.
    You must annotate the class or classes with JPA annotations.

  • Using <exclude-unlisted-classes /> or <exclude-unlisted-classes>true</exclude-unlisted-classes> indicates that the provider should ignore classes not
    specified with <jar-file> or <class>. Omitting <exclude-unlisted-classes> or using
    <exclude-unlisted-classes>false</exclude-unlisted-classes> causes the JPA
    provider to scan the classpath location of the persistence file for JPA-annotated classes. If
    persistence.xml is located in a JAR file, that JAR file (and only that JAR file) is scanned
    for classes. If persistence.xml is located in a directory-based classpath location (such as /
    WEB-INF/classes), that directory (and only that directory) is scanned for classes. Prior to
    Hibernate 4.3.0 and Spring Framework 3.2.5, specifying this element with the value false
    was incorrectly interpreted as true.

  • <shared-cache-mode> indicates how entities are cached in the persistence unit (if the JPA
    provider supports caching, which is optional). NONE disables caching, whereas ALL enables
    caching for all entities. ENABLE_SELECTIVE means that only entities annotated @javax
    .persistence.Cacheable or @Cacheable(true) (or marked as cacheable in orm.xml)
    are cached. DISABLE_SELECTIVE results in caching of all entities except those annotated
    @Cacheable(false) (or marked as non-cacheable in orm.xml). The default value,
    UNSPECIFIED, means that the JPA provider decides what the effective default is. Hibernate
    ORM defaults to ENABLE_SELECTIVE, but relying on this is not portable.

  • <validation-mode> indicates if and how Bean Validation should be applied to entities.
    NONE means that Bean Validation is not enabled, whereas CALLBACK makes the provider
    validate all entities on insert, update, and delete. AUTO has an effective value of CALLBACK
    if a Bean Validation provider exists on the classpath and an effective value of NONE if no
    Bean Validation provider exists on the classpath. If you enable validation, the JPA provider
    configures a new Validator to validate your entities. If you have configured a special
    Spring Framework Validator with your custom localized error codes, the JPA provider
    ignores it. As such, it’s best to set the validation mode to NONE and use Bean Validation
    before your persistence layer is invoked.

  • <properties> provides a way to specify other JPA properties, including standard JPA
    properties (such as JDBC connection string, username, and password, or schema generation
    settings) as well as provider-specific properties (such as Hibernate settings). You specify
    one or more properties using nested elements, each with a name and value
    attribute.

Nicholas S. Williams, Professional Java for Web Applications, (Indianapolis, Indiana: John Wiley & Sons, Inc., 2014), pg 584-585

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