Persistence,EntityManagerFactory和EntityManager
持久化单元(persist unit)就是关于一组Entity的命名配置。持久化单元是一个静态概念。
持久化上下文(Persist Context)就是一个受管的Entity实例的集合。
每一个持久化上下文都关联一个持久化单元,持久化上下文不可能脱离持久化单元独立存在。
持久化上下文是一个动态概念。
尽管持久化上下文非常重要,但是开发者不直接与之打交道,持久化上下文在程序中是透明的,我们通过EntityManager间接管理它。
2.三个类:Persistence,EntityManagerFactory和EntityManager
实体管理器工厂 EntityManagerFactory是获得实体管理器EntityManager对象的入口,
而EntityManagerFactory对象是通过javax.persistenc.Persistence类中的静态方法createEntityManagerFactory来创建的。
- javax.persistenc.Persistence类提供了两个手动创建EntityManagerFactory对象的方法,它们的定义如下所示。
package javax.persistence;
import java.util.*;
public class Persistence {
public static EntityManagerFactory createEntityManagerFactory(
String persistenceUnitName) {...}
public static EntityManagerFactory createEntityManagerFactory(
String persistenceUnitName, Map properties) {...}
}
- 其中,createEntityManagerFactory(String persistenceUnitName, Map properties)方法中properties的参数将覆盖persistence.xml文件配置的参数。
package javax.persistence;
import java.util.Map;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.criteria.CriteriaBuilder;
/**
* Interface used to interact with the entity manager factory
* for the persistence unit.
*
* <p>When the application has finished using the entity manager
* factory, and/or at application shutdown, the application should
* close the entity manager factory. Once an
* <code>EntityManagerFactory</code> has been closed, all its entity managers
* are considered to be in the closed state.
*
* @since Java Persistence 1.0
*/
public interface EntityManagerFactory {
/**
* Create a new application-managed <code>EntityManager</code>.
* This method returns a new <code>EntityManager</code> instance each time
* it is invoked.
* The <code>isOpen</code> method will return true on the returned instance.
* @return entity manager instance
* @throws IllegalStateException if the entity manager factory
* has been closed
*/
public EntityManager createEntityManager();
/**
* Create a new application-managed <code>EntityManager</code> with the
* specified Map of properties.
* This method returns a new <code>EntityManager</code> instance each time
* it is invoked.
* The <code>isOpen</code> method will return true on the returned instance.
* @param map properties for entity manager
* @return entity manager instance
* @throws IllegalStateException if the entity manager factory
* has been closed
*/
public EntityManager createEntityManager(Map map);
/**
* Return an instance of <code>CriteriaBuilder</code> for the creation of
* <code>CriteriaQuery</code> objects.
* @return CriteriaBuilder instance
* @throws IllegalStateException if the entity manager factory
* has been closed
*
* @since Java Persistence 2.0
*/
public CriteriaBuilder getCriteriaBuilder();
/**
* Return an instance of <code>Metamodel</code> interface for access to the
* metamodel of the persistence unit.
* @return Metamodel instance
* @throws IllegalStateException if the entity manager factory
* has been closed
*
* @since Java Persistence 2.0
*/
public Metamodel getMetamodel();
/**
* Indicates whether the factory is open. Returns true
* until the factory has been closed.
* @return boolean indicating whether the factory is open
*/
public boolean isOpen();
/**
* Close the factory, releasing any resources that it holds.
* After a factory instance has been closed, all methods invoked
* on it will throw the <code>IllegalStateException</code>, except
* for <code>isOpen</code>, which will return false. Once an
* <code>EntityManagerFactory</code> has been closed, all its
* entity managers are considered to be in the closed state.
* @throws IllegalStateException if the entity manager factory
* has been closed
*/
public void close();
/**
* Get the properties and associated values that are in effect
* for the entity manager factory. Changing the contents of the
* map does not change the configuration in effect.
* @return properties
* @throws IllegalStateException if the entity manager factory
* has been closed
*
* @since Java Persistence 2.0
*/
public Map<String, Object> getProperties();
/**
* Access the cache that is associated with the entity manager
* factory (the "second level cache").
* @return instance of the <code>Cache</code> interface
* @throws IllegalStateException if the entity manager factory
* has been closed
*
* @since Java Persistence 2.0
*/
public Cache getCache();
/**
* Return interface providing access to utility methods
* for the persistence unit.
* @return <code>PersistenceUnitUtil</code> interface
* @throws IllegalStateException if the entity manager factory
* has been closed
*
* @since Java Persistence 2.0
*/
public PersistenceUnitUtil getPersistenceUnitUtil();
}
- 独立运行环境下:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA");
EntityManager entityManger = emf.createEntityManager();
- 使用容器的环境下,不需要通过Persistence创建EntityManagerFactory和EntityManger,而是通过注解实现
@PersistenceContext
private EntityManger entityManger;
@PersistenceUnit
private EntityMangerFactory emf;
- 实体管理器EntityManager是负责管理Entity的对象。对Entity的操作包括添加、删除、修改和查询,都是通过实体管理器来实现的。
依赖注入EntityManager
- 在EJB容器 中,EntityManager的获得可以通过标注,使用依赖注入来创建EntityManager实例,代码如下所示。
Public class sessionbean1{
@PersistenceContext
EntityManager em;
。。。
}
- 通过将@PersistContext注解标注在EntityManager类型的字段上,这样得到的EntityManager就是容器管理的EntityManager,我们不需要也不应该显式的关闭注入的EntityManager实例。
- @PersistenceContex 表示标注的属性entityManager是一个实体管理器EntityManager对象,EJB容器会根据unitName的值来初始化 EntityManager。
- 注意:如果persistence.xml文件中配置了多个<persistence-unit>。
- 那么在注入EntityManager对 象时必须指定持久化名称,通过@PersistenceContext注释的unitName属性进行指定,例:
@PersistenceContext(unitName="foshanshop")
EntityManager em;
- 如果只有一个<persistence-unit>,不需要明确指定。
- 请注意:Entity Bean被EntityManager管理时,EntityManager会跟踪他的状态改变,在任何决定更新实体Bean的时候便会把发生改变的值同步 到数据库中(跟hibernate一样)。
- 但是如果entity Bean从EntityManager分离后,他是不受管理的,EntityManager无法跟踪他的任何状态改变。
容器管理的EntityManager类型
- 容器管理的EntityManager细分为两种类型:事务类型和扩展类型。
如果@PersistContext未指定type属性,或者指定为PersistContextType.TRANSACTION,则表示该类型是事务类型的。
如果指定为PersistContextType.EXTENDED,表示该EntityManager是扩展类型的。
- 事务类型:事务类型的EntityManger是无状态的,可用在无状态会话bean和有状态会话bean。
- 事务类型的EntityManger依赖于JTA,每次调用EntityManager实例的方法时,EntityManager会查看是否某个持久化上下文与当前事务关联,
- 如果有,则使用该持久化上下文,如果没有,EntityManager会创建一个持久化上下文,并将该持久化上下文与当前事务关联,事务结束时持久化上下文消失。
JPA支持两种类型的事务:本地资源事务(RESOURCE_LOCAL)和JAVA事务API(JTA)
本地资源事务(RESOUCE_LOCAL):使用JDBC驱动管理的本地事务。
JAVA事务API(JTA):可用于管理分布式事务,管理多数据源的情况。
容器管理的EntityManager总是使用JTA事务,应用程序管理的EntityManager可以使用本地资源事务,也可以使用JTA事务:
在j2SE环境下默认是本地资源事务,在J2EE环境下,默认是JTA。事务的类型在persistence.xml中定义。
- 扩展类型:扩展类型的EntityManager只能用于有状态会话Bean。
- 扩展的EntityManager在有状态会话bean实例创建时候创建一个持久化上下文,直到该有状态会话bean销毁时,相应的持久化上下文才被移除。
- 在扩展的EntityManager中,每次方法调用使用的都是相同的持久化上下文,所以前一次方法调用产生的受管实体下一个方法访问时仍然是受管实体。
Persistence,EntityManagerFactory和EntityManager相关推荐
- 【转】No Persistence provider for EntityManager问题
NetBeans 开发 JPA 应用程序,使用 TopLink ,报如下错误: Exception in thread "main" javax.persistence.Persi ...
- No Persistence provider for EntityManager named JPA
No Persistence provider for EntityManager named JPA 配置的provider 找不到问题的解决 1.首先配置文件persistence.xml 必须在 ...
- Caused by: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction
这是笔者把jpa的默认事务关闭后删除数据一个接口爆出的异常信息"Caused by: javax.persistence.TransactionRequiredException: No E ...
- Spring管理的交易说明-第2部分(JPA)
在本系列的第一部分中 ,我展示了事务如何在普通JDBC中工作 . 然后,我展示了Spring如何管理基于JDBC的事务. 在本系列的第二部分中,我将首先展示事务如何在普通的JPA中工作. 然后展示Sp ...
- EntityManager、EntityManagerFactory
1.EntityManager An instance of the interface javax. persistence. Entity Manager is used to persist, ...
- JPA - Persistence与EntityManagerFactory
[1]Persistence类 Persistence 类是用于获取 EntityManagerFactory 实例. 该类包含一个名为 createEntityManagerFactory 的 静态 ...
- java 清空一个list数据库_java – JPA EntityManager删除数据库中的所有记录
我有一个插入我的数据库的Servlet.这工作正常.第二个Servlet显示第一个插入的内容,但每当我运行显示的Servlet时,我所有表中的所有记录都将被删除!我的JPA实现是 EclipseLin ...
- JPA中persistence.xml模板
持久化单元,持久化对象的集合. 事务类型:本地事务和全局事务.某些应用场合,只能使用全局事务. 有两个数据库,mysql和oracle.转账时扣钱从mysql进行,加钱是在oracle执行,怎样确保两 ...
- JPA的entityManager的find、getReference、persisit、remove方法的使用
场景 JPA入门简介与搭建HelloWorld(附代码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103473937 ...
最新文章
- 如何删除一个CSDN上自己上传的资源
- 知乎快捷取消我关注的问题chrome插件
- 【KEIL·单片机·扫盲贴】关于ARM单片机程序内存使用情况的细致讨论。
- 实录 | DSTC 8“基于Schema的对话状态追踪”竞赛冠军方案解读
- 2008年最新的100条经典句子
- revit java 接口_如何使用Revit API获取axis属性?
- Python多线程参考资料
- 数据安全_个人信息查询
- React Ant Design UI 表格时间列格式化 代码片段
- python免费课程400节-Python2 教程
- Android成长的幕后推手:工程师鲁宾
- 网易家居专访柯拉尼陶晓松:有所为 有所不为 争创领军品牌
- vfp保存为html,建立,生成,运行VFP菜单
- matlab画图函数双精度,Matlab中图像函数大全2_matlab函数大全
- Socket 简介及面试题
- LCD液晶显示屏颜色显示波长研究与总结?
- Redis RDB和AOF
- Springboot @Lazy注解
- Photoshop几何3D立体头像制作教程
- 测试用例颗粒度实例列举
热门文章
- 视频教程-虚幻4 VR开发指南-其他
- IC-14W网络IC卡读写器_银河麒麟桌面操作系统V10适配测试报告
- Spring循环依赖解决思路
- 前端百题斩【019】——数组中方法原理早知道
- 前端百题斩【027】——解决跨域的常用利器CORS全解
- 百融金服榕树FICO信用评分,百融金服榕树数据+模型服务
- ShellExecute与ShellExecuteEx的用法
- HTML音频audio
- GPS、WiFi、基站、AGPS几种定位原理介绍与区别
- 江苏大学计算机组成原理课设6,计算机组成原理课程设计报告江苏大学适用于软件工程...