简体   繁体   English

使用JPA时,persistence.xml中的属性标签

[英]property tags in persistence.xml when using JPA

I am new to JPA and use Hibernate as the JPA provider. 我是JPA的新手,并使用Hibernate作为JPA提供程序。 I came to know that we need META-INF/persistence.xml configuration file. 我知道我们需要META-INF/persistence.xml配置文件。

I successfully created a simple Java program to persist data in DB using JPA. 我成功创建了一个简单的Java程序,使用JPA将数据持久存储在DB中。

All fine, doubts started when I looked into the persistence.xml file to understand it better. 当我查看persistence.xml文件以更好地理解它时,一切都开始令人怀疑。

Sample below: 示例如下:

<persistence-unit name="test-jpa" transaction-type="RESOURCE_LOCAL">
    <properties>
        <property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
        <property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/~/test"/>
        <property name="hibernate.connection.username" value="sa" />
        <property name="hibernate.connection.password" value="" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

The following is the Java code for reading the configuration: 以下是用于读取配置的Java代码:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("test-jpa");

The following are the doubts: 以下是疑问:

  1. How do we know that Hibernate is the JPA provider? 我们如何知道Hibernate是JPA提供者? Is it inferred by seeing the property tags in the file? 是否通过查看文件中的属性标签来推断?

  2. In config file, there are many <property> tags, are they pre-defined which can appear in the file (for a given JPA provider) or can we randomly add any property? 在配置文件中,有许多<property>标记,它们是预定义的,可以在文件中显示(对于给定的JPA提供程序),还是可以随机添加任何属性? who reads those <property> tags? 谁读取这些<property>标签?

A JPA provider would provide documentation that would tell you all of that. JPA提供程序将提供可以告诉您所有这些内容的文档。 Doesn't yours? 不是你的吗 I'd be surprised. 我会感到惊讶。

  1. You should either have a <provider> element in the persistence-unit to define which provider to use, or it would use the default for the environment that you are running in (in JavaSE you would need to have 1 and only one JPA provider in the CLASSPATH, in JavaEE the server would have its own default). 您应该在持久性单元中具有一个<provider>元素来定义要使用的提供程序,或者它将对运行环境使用默认设置(在JavaSE您将需要1个并且只有一个JPA提供程序。 CLASSPATH,在JavaEE ,服务器将具有自己的默认值)。

  2. They are provider-specific. 它们是特定于提供程序的。 Any properties that are prefixed javax.persistence would be JPA STANDARD. 前缀为javax.persistence任何属性都是JPA STANDARD。 The first 4 of those posted have javax.persistence variants that you should have used instead. 发布的文章中的前4个应该使用javax.persistence变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM