EJB读书笔记 关于EJB持久层的Managed/Unmanaged Entities

今天读的EJB的书,看到介绍持久层的一章。其中EJB的持久层由EntityManager机制实现。当一个Entity对象未attach到一个EntityManager上的时候是未被管理(处理)的,这时仍然是一个POJO(plain old Java objects ),对于Entity的改变不会引起数据库的变化。当Entity被attached到一个EntityManager以后,EntityManager跟踪实体对象的变化,并将此变化数据库同步。

持久层上下文是受管理的实体对象的集合。 它们被EntityManager管理。Entity Manager跟踪一个持久层上下文中的所有实体对象的变化,并于数据库同步。当这个持久层上下文关闭后,所有受管理的实体对象都被分离,不再是受管理的了。 一个对象从上下文中分离出来也就不再受EntityManager的管理,它的状态变化也就无法作用与数据库持久层。

而持久层上下文又分为两类: transaction-scoped和extended persistence contexts。这里就不详细介绍。

对于detached的实体对象EJB3.0较EJB2.1有所不同,原文是现下面这样介绍的。大体意思就是说:在Entity实例变得unmanaged的时候,实体对象可以被串行化并通过网络传输到客户端,从而可以实现交互。而在EJB2.1中要通过getFirstName( ), getLastName( )等等来实现数据的读取和串行化。而EJB3.0不再需要这种模式,因为在实体对象脱离持久层上下文之后自动转变成了数据对象。具体的看下面的英文吧呵呵。

Entity instances become unmanaged and detached when a transaction scope or extended persistence context ends. An interesting side effect is that detached entities can be serialized and sent across the network to a remote client. The client can make changes remotely to these serialized object instances and send them back to the server to be merged back and synchronized with the database.

This behavior is very different from the EJB 2.1 entity model, where entities are always managed by the container. In EJB 2.1, applications using entity beans always had a proxy to the entity bean; in EJB 3.0, you work with concrete instances of plain Java classes. For EJB 2.1 developers, this behavior will seem strange at first, since you are used to the container managing every aspect of the entity. You'll find that after you get used to the new EJB 3.0 model, your application code actually shrinks and is easier to manage.

EJB 2.1 code often used the Value Object Pattern (often called Data Transfer Objects). The idea of this pattern was that the entity bean exposed a method that copied its entire state into an object that could be serialized to remote clients (like a Swing application) that needed access to the entity's state:

// EJB 2.1 Entity bean class
public class CustomerBean implements javax.ejb.EntityBean {
CustomerValueObject getCustomerVO( ) {
return new CustomerValueObject(getFirstName( ), getLastName( ),
getStreet( ), getCity( ), getState, getZip( ));
}
}
It is very expensive to make a remote method call to an entity bean from a client. If the client had to call getFirstName( ), getLastName( ), etc., to get information about a customer it was displaying, performance would suffer. This is where the Value Object Pattern came in. EJB 3.0 eliminates the need for this pattern because persistent objects become value objects automatically when they are detached from a persistent context.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值