PO和VO的区别与关系

PO即Persistence Object
VO即Value Object

一个vo就是一个普通的对象,对应到对象状态当中是transient, detached两种状态。

Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated
with a Hibernate Session. It has no persistent representation in the database and no identifier value has
been assigned. Transient instances will be destroyed by the garbage collector if the application doesn't hold
a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of
the SQL statements that need to be executed for this transition).
• Detached - a detached instance is an object that has been persistent, but its Session has been closed. The
reference to the object is still valid, of course, and the detached instance might even be modified in this
state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the
modifications) persistent again. This feature enables a programming model for long running units of work
that require user think-time. We call them application transactions, i.e. a unit of work from the point of
view of the user.

PO 即纳入Hibernate管理框架中的VO,对应对象的persistent状态。

• Persistent - a persistent instance has a representation in the database and an identifier value. It might just
have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any
changes made to an object in persistent state and synchronize the state with the database when the unit of
work completes. Developers don't execute manual UPDATE statements, or DELETE statements when an object
should be made transient.

VO和PO的主要区别在于:
1。 VO是独立的Java Object。
2。PO是由Hibernate纳入其实体容器(Entity Map)的对象,它代表了与数
据库中某条记录对应的Hibernate实体,PO的变化在事务提交时将反应到实际数据库中。
如果一个PO与Session对应的实体容器中分离(如Session关闭后的PO),那么此时,它又会变成一个VO。

由PO、VO的概念,又引申出一些系统层次设计方面的问题。如在传统的MVC架构中,
位于Model层的PO,是否允许被传递到其他层面。由于PO的更新最终将被映射到实
际数据库中,如果PO在其他层面(如View层)发生了变动,那么可能会对Model
层造成意想不到的破坏。

O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写。通俗点讲,就是将对象与关系数据库绑定,用对象来表示关系数据。在O/R Mapping的世界里,有两个基本的也是重要的东东需要了解,即VO,PO。
  VO,值对象(Value Object),PO,持久对象(Persisent Object),它们是由一组属性和属性的get和set方法组成。从结构上看,它们并没有什么不同的地方。但从其意义和本质上来看是完全不同的。

1.VO是用new关键字创建,由GC回收的。
  PO则是向数据库中添加新数据时创建,删除数据库中数据时削除的。并且它只能存活在一个数据库连接中,断开连接即被销毁。

2.VO是值对象,精确点讲它是业务对象,是存活在业务层的,是业务逻辑使用的,它存活的目的就是为数据提供一个生存的地方。
  PO则是有状态的,每个属性代表其当前的状态。它是物理数据的对象表示。使用它,可以使我们的程序与物理数据解耦,并且可以简化对象数据与物理数据之间的转换。

3.VO的属性是根据当前业务的不同而不同的,也就是说,它的每一个属性都一一对应当前业务逻辑所需要的数据的名称。
  PO的属性是跟数据库表的字段一一对应的。

PO对象需要实现序列化接口。

VO: value object 值对象
PO: persistent object 持久化对象
BO: business object 业务对象
还有如DAO(Data Access Object) POJO (Plain Ordinary Java Object)

  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓