读书笔记: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
层造成意想不到的破坏。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值