EntityManagerFactory和EntityManager的一个用法探究

前段时间设计一个JAVA服务程序,用JPA作为数据库访问接口层。

JPA的主要工具,就是EntityManagerFactory和EntityManager了。其中,EntityManagerFactory是构建EntityManager的工厂,EntityManager可以操作数据库。

数据库的主要操作,其实是增删改查。目前,使用EntityManager的增删改查的方法如下所示:

查询

CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); //em为EntityManager
CriteriaQuery<Test> crit = criteriaBuilder.createQuery(Test.class);
Root<Test> root = crit.from(Test.class);
crit.select(root);

//Predicate对应搜索条件
Predicate predicate = criteriaBuilder.and(
	criteriaBuilder.equal(root.get("inuse"), true)
);

//查询结果
test = em
	.createQuery(crit.where(predicate))
	.getResultList();

增加或更改

//em为EntityManager
em.getTransaction().begin();

//test为实体类,merge将实现存在更改,不存在添加,以主键判断为准
em.merge(test);

em.getTransaction().commit();

删除

//em为EntityManager
em.getTransaction().begin();

//test为实体类,remove将实现删除
em.remove(test);

em.getTransaction().commit();

值得注意的是,对于复杂语句,如join,JPA没有做较好的支持。笔者试了下,较好的方法只能是自己动手写SQL语句,执行如下过程:

NativeQueryImpl queryImpl =  em.createNativeQuery(sql).unwrap(NativeQueryImpl.class);
queryImpl.setResultTransformer(Transformers.aliasToBean(Test.class));

Test db = (Test)queryImpl.getSingleResult();

最开始,在执行这些语句时,发现的过程从来都是创建EntityManager和关闭EntityManager。

发现EntityManager底层是用了连接池保持连接,发现数据库访问其实没多少,就试着把同一地址的数据存在一个Map里,当同一URL时,进行执行访问。

发现该方法:访问确实还算高效,但经常访问不到数据。

找到EntityManager和EntityManagerFactory的官方文档进行学习。

EntityManagerFactory关于EntityManagerFactory的介绍为:

“Interface used to interact with the entity manager factory for the persistence unit.

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 EntityManagerFactory has been closed, all its entity managers are considered to be in the closed state.”

就是EntityManagerFactory是持久化单元中与实体管理进行交互的接口。EntityManagerFactory主要用来管理EntityManager,并当EntityManagerFactory关闭时,EntityManager也会关闭。

而EntityManager的官方文档介绍为:

“Interface used to interact with the persistence context.

An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed. The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.

The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit. A persistence unit defines the set of all classes that are related or grouped by the application, and which must be colocated in their mapping to a single database.”

基本含义是:EntityManager实例(就是EntityManager实际是通过实例来体现作用)用与和持久化环境交互。持久化环境是实体的集合。EntityManager的API用于增删改查(基于持久化环境这个概念)。持久化单元能够管理持久化实例集合。持久化单元管理需要一个已经定义好的数据库映射来完成管理

有了这个官方文档,笔者就理解了为什么用EntityManagerFactory时需要persistence.xml。因为持久化层的定义、基本配置,还有数据库关系全在persistence.xml里。

还有,这个官方文档的说明其实还有:EntityManager是具体的操作增删改查,但EntityManagerFactory才定义了持久化存储单元的环境。

于是,可以用的方法是:EntityManagerFactory存储到Map里,然后生成EntityManager进行操作增删改查。

值得注意的是:想让操作正确,必须保证EntityManager的操作是单任务的,于是,synchronized关键字成为一个更好的选择。

测试了,用这种方式作为持久化环境的接口,效率还是挺高的。1000次访问,300秒完成(每次访问在0.5秒作用,符合网络访问要求)。

参考文献:

[1]EntityManagerFactory (Java(TM) EE 7 Specification APIs)

[2]EntityManager (Java(TM) EE 7 Specification APIs)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值