EntityManager 实例化方法
Configure the EntityManager via a persistence.xml file
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="movie-unit">
<jta-data-source>movieDatabase</jta-data-source>
<non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
<class>org.superbiz.injection.jpa.Movie</class> <properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>
Notice that the Movie
entity is listed via a <class>
element. This is not required,
but can help when testing or when the Movie
class is located in a different jar than the jar containing the persistence.xml
file.
persistence.xml 和 Entity在同一个jar包下的话,则不需要标注class元素。
Injection via @PersistenceContext
The EntityManager
itself is created by the container using the information in the persistence.xml
, so to use it at runtime, we simply need to request it be injected into one of our components. We do this via @PersistenceContext
The @PersistenceContext
annotation can be used on any CDI bean, EJB, Servlet, Servlet Listener, Servlet Filter, or JSF ManagedBean. If you don't use an EJB you will need to use a UserTransaction
begin and commit transactions manually. A transaction is required for any of the create, update or delete methods of the EntityManager to work.
package org.superbiz.injection.jpa; import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import java.util.List; @Stateful
public class Movies { @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
private EntityManager entityManager; public void addMovie(Movie movie) throws Exception {
entityManager.persist(movie);
} public void deleteMovie(Movie movie) throws Exception {
entityManager.remove(movie);
} public List<Movie> getMovies() throws Exception {
Query query = entityManager.createQuery("SELECT m from Movie as m");
return query.getResultList();
}
}
This particular EntityManager
is injected as an EXTENDED
persistence context, which simply means that the EntityManager
is created when the @Stateful
bean is created and destroyed when the @Stateful
bean is destroyed. Simply put, the data in the EntityManager
is cached for the lifetime of the @Stateful
bean.
The use of EXTENDED
persistence contexts is only available to @Stateful
beans. See the JPA Concepts page for an high level explanation of what a "persistence context" really is and how it is significant to JPA.
EntityManager 实例化方法的更多相关文章
- Android多媒体--MediaCodec的实例化方法
*由于作者水平限制,文中难免有错误和不恰当之处,望批评指正. *转载请注明出处:http://www.cnblogs.com/roger-yu/ MediaCodec的实例化方法主要有两种: 1.使用 ...
- 面向对象之静态方法(static)和实例化方法的区别
这是一个经常被时时提出来的问题,很多时候我们以为理解了.懂了,但深究一下,我们却发现并不懂. 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法,问原因也说不出来所以然,或者简 ...
- PHP中静态方法和实例化方法的区别
在PHP中类为什么要使用静态方法,有什么好处 不需要实例化?? 可以提高运行效率?? 这是一个经常被时时提出来的问题,很多时候我们以为理解了.懂了,但深究一下,我们却发现并不懂. 方法是我们每天都在写 ...
- Android MediaCodec 的实例化方法
*由于作者水平限制,文中难免有错误和不恰当之处,望批评指正. *转载请注明出处:http://www.cnblogs.com/roger-yu/ MediaCodec的实例化方法主要有两种: 1.使用 ...
- java静态方法和实例化方法的区别(copy)
[资料来源] http://blog.csdn.net/biaobiaoqi/article/details/6732117 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法 ...
- JPA技术之EntityManager使用方法
Session bean or MD bean对Entity bean的操作(包括所有的query, insert, update, delete操作)都是通过EntityManager实例来完成的. ...
- php类的定义与实例化方法
php类的定义 类是对某个对象的定义.它包含有关对象动作方式的信息,包括它的名称.方法.属性和事件.实际上它本身并不是对象,因为它不存在于内存中.当引用类的代码运行时,类的一个新的实例,即对象,就在内 ...
- Java系列之:看似简单的问题 静态方法和实例化方法的区别
(2011-12-06 15:28:26) 转载▼ 标签: 杂谈 分类: study 今天看书时一直没真正弄明白静态方法和实例方法的区别,就在网上搜索,找到一篇很好的文章,和大家分享一下: 这是一个经 ...
- Python笔记_第四篇_高阶编程_实例化方法、静态方法、类方法和属性方法概念的解析。
1.先叙述静态方法: 我们知道Python调用类的方法的时候都要进行一个实例化的处理.在面向对象中,一把存在静态类,静态方法,动态类.动态方法等乱七八糟的这么一些叫法.其实这些东西看起来抽象,但是很好 ...
随机推荐
- laravel5.2之logout注销账号无效
问题描述:laravel5.2的框架,使用框架auth用户认证后,进行账号注销退出的时候,无法实现. 只有清除浏览器缓存,才能实现账号退出. 解决办法: 改变路由 Route::get('auth/l ...
- Windows平台下python2和3的兼容问题解决
很多朋友都安装了python2和3,因为用些库例如scapy,不是scrapy,python3下面都是错,那么怎么让python2和3共存呢. 像一般的程序员,达到如下效果 Windows平台下的兼容 ...
- node async基础1
async的基础使用 1 async each 语法格式each(collection, iteratee, [callback]) 用途:遍历集合中的元素,并行对每个元素执行一定的操作,但是 ...
- 对yield 的理解
最近在学习Python的时候看到yield的相关语法,感觉很独特,相比其他如C/C++的语法比较有意思,于是在看完资料相关章节做一个总结. yield 是一个类似于 return的语法,但是对于ret ...
- 全景技术大揭秘,市场核心早洞悉——VR全景加盟
未来已来,未来已见.2017是3D全景创业的天时,全景行业逐步走向成熟.全景智慧城市专注vr全景6年,技术国内遥遥领先.全景智慧城市市场总监常诚,透漏3D全景技术和市场的核心. 拍摄全景必备的设备:单 ...
- redis集群搭建实践
参考 第一个节点 第一个节点为本地的机器 IP:192.168.23.148 检查机器配置 $ uname -a Linux wangya-Lenovo-G480 4.8.0-52-generic # ...
- CentOS7安装PostgreSQL9.4
这次选择的数据库安装的是run 文件,更容易掌握.这次数据库全是默认安装,如果有需求的可以自行修改一下的. 这是我的第一篇博客,各位观众老爷,如果觉得哪里有什么不好的,可以留言一起探讨,探讨.有什么问 ...
- 【MyBatis源码解析】MyBatis一二级缓存
MyBatis缓存 我们知道,频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO操作速度相比内存操作速度慢了好几个量级),尤其是对于一些相 ...
- 最新开源DBLayer,原来数据库操作可以这么简单
DBLayer,我最近开源的数据库轻量级orm框架,目前支持sqlserver.mysql.oracle, 特别做了分页的封装. 这个框架从七八年前开始逐渐升级而来,也经历了不少项目,希望可以将大家从 ...
- Cordova各个插件使用介绍系列(一)—$cordovaSms发送短信
详情链接地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/cordova-1-cordovasms/ 这是调用手机发送短信的插件 ...