PersistenceUnit、PersistenceContext

PersistenceUnit

什么是持久化单元?

持久化单元是一个逻辑分组,其中包含诸如EntityManagerFactory 的配置、一组实体类、映射元数据(可以通过扫描映射注释或从META-INF 目录下的persistence.xml/orm.xml 加载)等信息。每个持久化单元必须有一个唯一的名称。一个应用程序可以有一个或多个持久化单元。

持久化文件

持久化单元的根由类路径中 META-INF 目录下的 persistence.xml 文件定义。独立应用程序中典型的 persistence.xml 如下所示:

<persistence>
 <persistence-unit name="...." transaction-type="...">
  <provider>...</provider>
  <exclude-unlisted-classes>true/false</exclude-unlisted-classes>
  <properties>
   <property name="javax.persistence.schema-generation.database.action" value="..."/>
   <property name="javax.persistence.jdbc.url" value="..."/>
  </properties>
  <mapping-file>...</mapping-file>
  <class>...</class>
  <class>...</class>
 </persistence-unit>
</persistence>

相关参数:

  • name: A unique name for persistence unit. It is used to create the instance of EntityManagerFactory by using Persistence. create Entity Manager Factory as shown in above code snippet.
  • transaction-type: Possible values are JTA (used in Java EE environment) and RESOURCE_LOCAL. If RESOURCE_LOCAL is specified then the application is responsible to do transaction management manually, typically like this:
        entityManager.getTransaction().begin();
        entityManager.persist(entity);
                ......
        entityManager.getTransaction().commit();
            
  • provider: This specifies the qualified name of the persistence provider's javax.persistence.spi.PersistenceProvider implementation, e.g. if we are using EclipseLink then it would be: org.eclipse.persistence.jpa.PersistenceProvider
  • exclude-unlisted-classes: If this is set to false then entity classes will be discovered by scanning annotations, otherwise, we have to list all entity classes specified explicitly by <class></class> elements.
  • properties: This element can be used to specify both standard and vendor-specific properties. For example, the standard property javax. persistence. schema-generation. database . action specifies the action to be taken by the persistence provider with regard to the database schema automatic generation. The possible values for this property are none/create/drop-and-create/drop. If this property is not specified or is none, no schema generation actions will be taken on the database.
  • mapping-file: This is to specify one or more object/relational mapping XML files, if we do not want to specify all of them in 'this' persistence.xml file.

PersistenceContext

什么是持久化上下文?

EntityManager 实例与persistence-context相关联。持久上下文是一组托管的唯一实体实例。EntityManger 与此上下文交互以管理实体实例及其生命周期。

EntityManager 不是线程安全的,所以我们应该每个线程只使用一个实例。

每个 EntityManagerFactory 实例都提供 EntityManager 实例,这些实例都以相同的方式配置,即都将使用相同的持久性单元。每个应用程序可以使用多个 EntityManagerFactory 实例,这些实例可能指向不同的实体集和数据源等。

当应用程序完成使用实体管理器工厂和/或应用程序关闭时,应用程序应通过调用emf.close(). 一旦实体管理器工厂关闭,从它创建的所有实体管理器也被视为处于关闭状态。

详情参考:https://www.logicbig.com/tutorials/java-ee-tutorial/jpa/entity-context.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值