2. What is persistence? Why persistence?1. What is persistence?
I have found a lot of articles and have not found any satisfactory answers. I finally read the following explanation from "proficient in hibernate: Java object persistence technology" written by Sun weiqin, I think it is complete. The excerpt is as follows:
Narrow understanding: "persistence" only refers to permanent storage of domain objects in the database. In a broad sense, "persistence" includes database-related operations (Persistence means to save useful data with a certain technology, and in the future, it can be used again for applications and database technologies, storing memory data as a file in permanent media (disks, etc.) is an example of persistence .).
● Save: Permanently Save the domain object to the database.
● Update: updates the status of domain objects in the database.
● Delete: delete a domain object from the database.
● Load: loads a domain object from the database to the memory based on a specific oid.
● Query: loads one or more domain objects that meet the query conditions from the database based on specific query conditions. 2. Why persistence?
The persistence technology encapsulates data access details and provides object-oriented APIs for most business logic.
● The Persistence technology can reduce the number of accesses to database data and increase the application execution speed;
● High code reusability, able to complete most database operations;
● Loose coupling makes persistence independent of the underlying database and upper-layer business logic. When changing a database, you only need to modify the configuration file instead of the Code.
Learning notes what is persistence and object relationship ORM ing ORM Technology
By Naven at 2005-09-19
What is "persistence"
Persistence: saves data (such as objects in memory) to a permanently retained storage device (such as a disk ). Persistence is mainly used to store data in the memory in a relational database. Of course, it can also be stored in disk files or XML data files.
What is "persistent layer"
Persistence Layer (persistence layer) is a logical layer that focuses on a specific system in the field of data persistence application. It associates data users with data entities.
What is "Object Data ing (ORM )"
Orm-Object/relational mapper, that is, "object-relational data ing component ". For o/R (object) and relational (relational data), it means that object-oriented and relational data must be used for development at the same time.
Note: The ORM in the modeling field is object/role modeling (object role modeling ). In addition, "O/R mapper" is used instead of "O/R Mapping ". O/R Mapping describes a design idea or implementation mechanism, while o/R mapper refers to a persistence framework designed based on o/R principles ), it includes o/R, SQL self-generation, transaction processing, and Cache Management.
In addition to the ORM technology, there are several Persistence Technologies
Active Domain Object Mode
It encapsulates the relational data model and data access details in the implementation. In the J2EE architecture, EJB components are divided into session ejbs and entity ejbs. Session ejbs generally implement business logic, while entity ejbs represent business entities. The object EJB is divided into two types: The EJB itself manages persistence, that is, BMP (bean-managed persistence); The EJB container manages persistence, that is, CMP (container-managed persistence ). Bm p is an example of the active domain object mode. BMP indicates that the data access details are managed by the entity EJB itself.
The active domain object itself is located in the business logic layer. Therefore, when the active domain object mode is adopted, the entire application is still a three-tier application structure, and the independent persistence layer is not separated from the business logic layer.
JDO Mode
Java Data Objects (JDO) is a standard API developed by Sun to describe the persistence semantics of objects. Strictly speaking, JDO is not an object-link ing interface, because it supports persistence of objects to any storage system, including relational databases, object-oriented databases, XML-based databases, and other proprietary storage systems. Since relational databases are currently the most popular storage systems, many JDO implementations contain the object-link ing service.
CMP Mode
In the J2EE architecture, CMP (container-managed persistence) indicates that the EJB container manages the persistence of the entity EJB. The EJB container encapsulates the ing of Object-link and data access details. The similarities between CMP and ORM are that both provide object-relationship ing services, which separate Object Persistence tasks from the business logic. The difference is that CMP is responsible for the persistence of the entity EJB component, while ORM is responsible for the persistence of pojo, which is a common entity domain object based on Java Bean.
Generally, the object in the form of Java Bean is called a pojo (plain old Java object), meaning the meaning of a common and ancient Java object. As various ORM Ing tools become more and more mature and popular, pojo has a re-emergence of brilliance. Compared with CMP-based entity ejbs, pojo is simple and highly portable, therefore, the combined use of ORM Ing tools and pojo has become an increasingly popular Persistence Solution to replace CMP. The disadvantage of pojo is that it cannot be called remotely and does not support distributed computing.
Why persistence and ORM design?
In the current enterprise application system design, MVC (model)-View-control) is the main system architecture mode. The model in MVC contains complex business logic and data logic, as well as data access mechanisms (such as JDBC connection, SQL generation, statement creation, and resultset result set reading. Separating these complex business logic and data logic to convert the system's tight coupling relationship into a loose coupling relationship (I .e., decoupling) is an urgent task to reduce the Coupling Degree of the system, it is also the task of persistence. The MVC mode implements the decoupling between the presentation layer (View) and the data processing layer (model) in the architecture, the persistence design implements the decoupling of business logic and data logic separation in the Data Processing Layer. As the most important and complex technology in persistence design, Orm is also a hot technology in the industry.
To put it simply, the business processing logic and data access logic are mixed in the general system design and JDBC operations on the database.
Generally, the steps are as follows:
1. Create a database connection to obtain the connection object.
2. Assemble and query SQL statements based on user input.
3. Create a statement object or preparedstatement object based on the SQL statement.
4. Run the SQL statement with the connection object to obtain the result set resultset object.
5. Read the data in the result set resultset object one by one.
6. Calculate the data according to the specific business logic.
7. Assemble and update SQL statements based on the calculated results.
8. Use the connection object to execute the update SQL statement to update data in the database.
7. Close each statement object and connection object in sequence.
It can be seen that the Code logic is very complex, which does not include the processing logic for failed statement execution. The business processing logic and data access logic are completely mixed. A complete system must contain thousands of such repeated and mixed processing processes. If you want to modify some of the business logic or related business processes, the amount of code to be modified is unimaginable. On the other hand, changing database products or running environments may also be an impossible task. However, the user's operating environment and requirements vary widely. We cannot design the same system for every user's running environment.
Therefore, we need to separate the same processing code, that is, the business logic from the data access logic that may be different. On the other hand, the data in the relational database is basically accessed in one row of data, while the program runs is processed by objects, most of the database-driven technologies (such as ADO) are currently used. net, JDBC, ODBC, and so on) are all processed in a row set result set. To solve this problem, the orm object and data ing technology emerged.
For example, if you want to complete a program for shopping discounts and promotions, you can use the ORM idea to implement the following (derived from Hibernate):
The business logic is as follows:
Public double calcamount (string customerid, double amount)
{
// Obtain the customer record based on the customer ID
Customer customer = customermanager. getcustomer (custmerid );
// Obtain discount rules based on customer levels
Promotion = promotionmanager. getpromotion (customer. getlevel ());
// Accumulate the total customer consumption and save the accumulated results
Customer. setsumamount (customer. getsumamount (). Add (amount );
Customermanager. Save (customer );
// Return the discounted amount
Return amount. Multiply (protomtion. getratio ());
}
In this way, the code is very clear and completely isolated from the data access logic. When designing the business logic code, you do not need to consider the same operations of database JDBC, but hand it over to customermanager and promotionmanager. This is a simple ORM design. The actual ORM implementation framework is much more complicated than this one.
What popular ORM products are currently available?
Currently, many vendors and open-source communities provide implementation of the persistent layer framework.
Apache OJB (http://db.apache.org/ojb)
Cayenne (http://objectstyle.org/cayenne)
Jaxor (http://jaxor.sourceforge.net)
Hibernate (the http://www.hibernate.org)
IBM (http://www.ibatis.com)
Jrelationalframework (http://ijf.sourceforge.net)
Mirage, http://itor.cq2.org/en/oss/mirage/toon)
Http://www.drjava.de/smyle (smyle)
Toplink (http://otn.oracle.com/products/ias/toplink/index.html)
Among them, toplink is a commercial Oracle product, and others are open-source projects.
Among them, Hibernate's lightweight ORM model gradually establishes a leading position in the Java ORM architecture, and even replaces the complex and complicated EJB model to become the de facto Java ORM industrial standard. Many of these designs have been absorbed by J2EE standards organizations and become the latest EJB 3.0 standard, which is also a strong witness to the impact of open-source projects on industrial standards.
References: 1. Introduction to hibernate
2. proficient in hibernate: detailed explanation of Java object persistence Technology