Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between Hibernate PO and Hibernate VO

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about the differences between Hibernate PO and Hibernate VO. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

PO (Persistence Object) and VO (Value Object) are two key concepts in Hibernate.

First of all, what is VO? it's very simple. VO is a simple value object.

Summary:

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, and generate

And execute the corresponding update statement.

3. If the reference does not exist, the insert operation is performed according to the mapping relationship.

A) in our example here, native's id generation mechanism is used, so hibernate will

Gets 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, before returning the object, Hibernate has already incorporated the object into the actual

In the body container.

The main differences between Hibernate VO and Hibernate PO are:

◆ 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).

Attribute replication can be done through the property batch copy function provided by the Apache Jakarta Commons Beanutils (http://jakarta.apache.org/commons/beanutils/) component to avoid complicated get/set operations.

Thank you for reading! This is the end of the article on "what are the differences between Hibernate PO and Hibernate VO". 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, you can 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report