In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what is the difference between VO and PO in Hibernate. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
VO, value objects (Value Object), PO, persistent objects (Persisent Object), which are made up of a set of properties and their get and set methods. Structurally, there is no difference between them. But from its meaning and essence, it is completely different.
1.VO is created with the new keyword and recycled by GC.
PO is created when new data is added to the database and deleted when the data in the database is deleted. And it can only survive in one database connection, disconnected and destroyed.
2.VO is a value object, to be exact, it is a business object, it lives in the business layer, it is used by business logic, and its purpose of survival is to provide a place for data to live.
PO is stateful, and each attribute represents its current state. It is an object representation of physical data. Using it, our program can be decoupled from physical data, and the conversion between object data and physical data can be simplified.
The attributes of 3.VO vary according to the current business, that is, each of its attributes corresponds to the name of the data required by the current business logic.
The properties of PO correspond to the fields of the database table one by one.
The PO object needs to implement the serialization interface.
VO is a stand-alone Java Object.
PO is an object incorporated into its entity container (Entity Map) by Hibernate, which represents the Hibernate entity corresponding to a record in the database. The change of PO will be reflected in the actual database when the transaction is committed. If a PO is separated from the entity container corresponding to Session (such as PO after Session is turned off), it becomes a VO again.
From the concepts of Hibernate VO and Hibernate PO, some problems in system level design are introduced. For example, in the traditional MVC architecture, whether the PO located in the Model layer is allowed to be passed to other levels. Since updates to PO will eventually be mapped to the actual database, if PO changes at other levels (such as the View layer), it may cause unexpected damage to the Model layer.
Therefore, in general, direct PO transfer to other levels in the system should be avoided. One solution is to pass this VO to other layers to achieve the necessary data transfer through an VO, which has the same attribute value as PO through attribute replication, and uses it as the transmission medium (in fact, this VO is used as a Data Transfer Object, that is, the so-called DTO).
VO is processed by Hibernate and becomes PO.
In session.save (user), we pass a VO "user" to Hibernate's Session.save method to save it. In the save method, Hibernate handles it as follows:
1. Query whether there is a reference to the user object in the entity container (Entity Map) corresponding to the current session.
2. If the reference exists, return the user object directly to end the id,save process. In Hibernate, there is an entity container (actually a Map object) for each Session, and if a reference to the target object is already stored in this container, the hibernate will assume that the object is already associated with the Session.
For save operations, no specific operation is required if the object is already associated with the Session (that is, it has been added to the entity container of the Session). Because later in the Session.flush process, Hibernate will traverse the objects in this entity container, find out the changed entities, generate and execute the corresponding update statements.
3. If the reference does not exist, the insert operation is performed according to the mapping relationship.
A) in our example, native's id generation mechanism is used, so hibernate takes the id generated by the insert operation from the database and assigns the id property of the user object.
B) include references to user objects into the entity container of Hibernate.
C) the save process ends and the object id.
In the Session.load method, Hibernate already includes the object in its entity container before it is returned.
This is the end of the article on "what is the difference between VO and PO in Hibernate". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.