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

你可能感兴趣的:(sql,mvc,框架,Hibernate,读书)