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 @Statefulbean is created and destroyed when the @Stateful bean is destroyed. Simply put, the data in the EntityManager is cached for the lifetime of the @Statefulbean.
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调用类的方法的时候都要进行一个实例化的处理.在面向对象中,一把存在静态类,静态方法,动态类.动态方法等乱七八糟的这么一些叫法.其实这些东西看起来抽象,但是很好 ...
随机推荐
- EM算法原理总结
EM算法也称期望最大化(Expectation-Maximum,简称EM)算法,它是一个基础算法,是很多机器学习领域算法的基础,比如隐式马尔科夫算法(HMM), LDA主题模型的变分推断等等.本文就对 ...
- Java如何转换protobuf-net中的bcl.DateTime对象
一.定义DateTime Message 参考文档:https://github.com/mgravell/protobuf-net/blob/master/src/Tools/bcl.proto m ...
- v$session & v$session_wait
(1)v$session v$session视图记录了当前连接到数据库的session信息 Column Description SADDR session address SID Session i ...
- 全景智慧城市——VR全景,开启VR营销新时代
全景是一种新兴的富媒体技术. 与视频.声音.图片等传统主流媒体最大的区别是"可操作,可交互". 全景给人以三维立体感觉的实景360°全方位图像,此图像最大的三个特点: 全方位:展示 ...
- Linux上open-iscsi 的安装,配置和使用
关于open-iscsi open-iscsi是一个实现 RFC3720 iSCSI协议的高性能initiator程序.iSCSI使得访问SAN上的存储不再只能依赖Fibre Channel,也可以通 ...
- yarn工作流程
YARN 是 Hadoop 2.0 中的资源管理系统, 它的基本设计思想是将 MRv1 中的 JobTracker拆分成了两个独立的服务 : 一个全局的资源管理器 ResourceManager 和每 ...
- MPP 二、Greenplum数据加载
Loading external data into greenplum database table using different ways... Greenplum 有常规的COPY加载方法,有 ...
- Android布局方式
1. LinearLayout(线性布局) android:orientation="vertical" android:layout_width="wra ...
- 在Quo.js下Tap和singleTap的区别
前两天上网搜开发手机页面的JS,看到了Quo.js下载下来后来看了一下,支持的触屏手势挺多的,在一般的开发中应该够用了,更让我喜欢它的一点是它跟JQ差不多(虽然功能不如JQ强大但是语法基本一致).这就 ...
- Windows窗口的尺寸和位置
介绍 窗口的大小和位置表示为一个矩形边界,该矩形的坐标是相对于屏幕或父窗口而言的.顶级窗口的坐标是相对于屏幕的左上角而言的,子窗口的坐标则是相对于父窗口的左上角而言.应用程序创建窗口时(CreateW ...