EntityManager
Obtaining an EntityManager in a SessionBean
Before an EntityManager can be used it must be obtained from the container. The EntityManager can be set into your SessionBean by the Container though the @Resource annotation.
@Stateless
public class EmployeeDemoSessionEJB implements EmployeeDemoSession {
@Resource protected EntityManager em;
...
public EntityManager getEntityManger(){
return this.em;
}
...
Whenever the SessionBean is accessed an EnityManger will be available in the em attribute.
The EntityManager can also be retreived through a JNDI lookup.
...
public EntityManager getEntityManager() {
if (em == null}
try{
em = (EntityManager)(new InitialContext()).lookup("java:comp/ejb/EntityManager");
} catch (Exception e){};
}
return em;
}
...
Note that in this case the @Resource annotation is not used.
EntityManager persist()
The EntityManager persist(Object entity) API is used to mark a new instance for insert into the Database. It must only be called on new Entities. The value returned from the persist(Object entity) call is the same instance as was passed in.
@Stateless
public class EmployeeDemoSessionEJB implements EmployeeDemoSession {
...
public void createEmployee(String fName, String lName) {
Employee employee = new Employee();
employee.setFirstName(fName);
employee.setLastName(lName);
em.persist(employee);
}
...
EntityManager merge()
To integrate a bean read in a different transaction or provided by the client into the current transaction use the merge(Object entity) API. The result will be an instance of the provided entity. The state of that entity will also be available in the returned instance.
@Stateless
public class EmployeeDemoSessionEJB implements EmployeeDemoSession {
...
public void updateAddress(Address addressExample) {
em.merge(addressExample);
}
...
EntityManager remove()
To delete a bean from the database use the remove(Object entityBean) API.
@Stateless
public class EmployeeDemoSessionEJB implements EmployeeDemoSession {
...
public void removeEmployee(Integer employeeId) {
Employee employee = (Employee)em.find("Employee", employeeId);
...
em.remove(employee);
}
...
EntityManager find()
When a primary key query is required the find(String entityName, Object primaryKey) or find(Class entityClass, Object primaryKey) API can be used.
@Stateless
public class EmployeeDemoSessionEJB implements EmployeeDemoSession {
...
public void removeEmployee(Integer employeeId) {
Employee employee = (Employee)em.find("Employee", employeeId);
...
em.remove(employee);
}
...
EntityManager flush()
The EntityManager flush() API is used to send updates to the database within a transaction, subsequent queries within the same transaction will return the updated data. This is usefull if a particular transaction spans mutiple operations or pages, similar to a "wizard".
@Stateless
public class EmployeeDemoSessionEJB implements EmployeeDemoSession {
...
public void terminateEmployee(EmploymentPeriod period, Integer employeeId){
Employee employee = (Employee)em.find("Employee", employeeId);
employee.setPeriod(period);
em.flush();
...
EntityManager refresh()
When the latest data is required or when changes need to be reverted in an Entity Bean the refresh(Object entity) API can be used.
...
public void undoUpdateEmployee(Integer employeeId){
Employee employee = (Employee)em.find("Employee", employeeId);
em.refresh(employee);
}
...
EntityManager createQuery()
In order to perform queries based on more complex criteria queries can be created using the createQuery(String ejbqlString) or createNamedQuery(String name) see the How To on Queries for more specific information.
@Stateless
public class EmployeeDemoSessionEJB implements EmployeeDemoSession {
...
public Collection findLargeProjectsWithBudgetLargerThan(double budget) {
Collection projects = em.createNamedQuery("findWithBudgetLargerThan")
.setParameter("amount", budget).getResultList();
return projects;
}
public Project findOneProjectByQuery(Vector params) {
Project project = (Project)em.createQuery("SELECT OBJECT(project) FROM Project project WHERE project.name = :projectName")
.setParameter("projectName", params.firstElement()).getSingleResult();
return project;
}
...
EntityManager createQuery(Expression)
If TopLink's java like expressions are prefered for the query criteria, that functionality is available though the createQuery(Expression expression, Class resultType) api on the OC4J specific EntityManager implementation oracle.toplink.ejb.cmp3.EntityManager.
@Stateless
public class EmployeeDemoSessionEJB implements EmployeeDemoSession {
...
public Collection findManyProjectsByQuery(Vector params) {
ExpressionBuilder builder = new ExpressionBuilder();
Query query = ((oracle.toplink.ejb.cmp3.EntityManager)em)
.createQuery(builder.get("name").equals(builder.getParameter("projectName")), Project.class);
query.setParameter("projectName", params.firstElement());
Collection projects = query.getResultList();
return projects;
}
...
分享到:
相关推荐
为了获取EntityManager实例,通常使用依赖注入(Dependency Injection)的方式。例如,在Session bean中,可以通过以下代码注入EntityManager: ```java @PersistenceContext EntityManager em; ``` 这里的`@...
这个Demo将展示如何使用EntityManager进行基本的数据操作,包括创建、查询、更新和删除实体。 【详细知识点】 1. **Java Persistence API (JPA)**:JPA是Java平台上的一个标准,用于管理和持久化Java对象到关系...
本文档为Hibernate 3.3.0.GA版本的EntityManager使用指南,主要介绍了如何在不同的环境中使用EntityManager管理持久化对象,包括实体的状态管理、查询操作、事务处理等内容。 #### 一、架构 ##### 1.1 定义 - **...
- **删除(Delete)**:使用`entityManager.remove(entity)`或`entityManager.remove(entity.getId())`删除实体。 7. **事务管理**:在执行完一组操作后,使用`entityManager.getTransaction().commit()`提交事务...
hibernate-entitymanager.jar hibernate-entitymanager.jar
hibernate entitymanager
使用`@Transactional`注解可以声明方法在一个事务中执行,或者在代码中显式调用`entityManager.getTransaction().begin()`和`entityManager.getTransaction().commit()`来控制事务。事务处理确保了数据的一致性。 ...
hibernate-entitymanager-5.5.6
本篇文章将深入探讨 Hibernate 实体管理器(Hibernate EntityManager)3.2版本的核心特性和使用方法。 Hibernate EntityManager 是 Hibernate 项目的一部分,它是一个符合 JPA(Java Persistence API)规范的 ORM ...
hibernate-entitymanager-5.5.5.Final
Session bean or MD bean对Entity bean的操作(包括所有的query, insert, update, delete操作)
jpa--7.api(EntityManager其他方法)jpa--7.api(EntityManager其他方法)jpa--7.api(EntityManager其他方法)jpa--7.api(EntityManager其他方法)
hibernate-entitymanager-5.5.3.Final
hibernate-entitymanager-5.6.0.Beta1
hibernate-entitymanager-5.5.7.Final
在【压缩包子文件的文件名称列表】中,只列出了 "hibernate-entitymanager-3.4.0.GA" 这个文件,这通常意味着压缩包包含的就是这个jar文件本身,开发者可以直接将其添加到项目的类路径中以使用Hibernate Entity ...
hibernate-entitymanager-3.4.0.GA.rar hibernate-entitymanager-3.4.0.GA.rar hibernate-entitymanager-3.4.0.GA.rar hibernate-entitymanager-3.4.0.GA.rar hibernate-entitymanager-3.4.0.GA.rar hibernate-...
配置hibernate jpa所需jar包,为hibernate的entitymanager等提供支持