This article compares the persistence Technology of common Java domain objects. This article introduces the concept of domain objects, discusses why persistence technology should be introduced, and compares the five common domain Object Persistence Technologies in Java, evaluate their respective advantages and disadvantages and applicability.
1. Layered Architecture of applications
The three-layer structure is a typical application software structure. The three layers are the presentation layer, business logic layer, and database layer. The presentation layer provides an interactive interface with the user. GUI and Web pages are two typical examples of the presentation layer. The business logic layer implements various business logic; the database layer stores and manages persistent data of applications.
In the preceding three-tier structure, the business logic layer is not only responsible for business logic, but also directly accesses the database, providing storage, update, deletion, and query operations for business data. If the database changes or the table structure of the database changes, the business logic layer will be greatly affected. To separate data access details from business logic, you can use data access as a separate persistence layer. The persistence layer encapsulates data access details and provides an object-oriented API for the business logic layer. The complete persistence layer should meet the following objectives:
? Code is highly reusable and can complete all database access operations;
? Supports multiple database platforms;
? It is relatively independent. When the persistence layer changes, the implementation of the upper layer is not affected.
2. Software Model
In the field of software development, models are used to represent entities in the real world. Different types of models must be created for the target system at different stages of software development. In the analysis phase, you must create a conceptual model. In the design phase, you must create a domain model and a data model.
The basic element of the Domain model is the Domain Object, which is an abstraction of the real-world entities. Domain objects can represent people, locations, transactions, or concepts in the business field. Domain objects include entity domain objects, process domain objects, and event domain objects. In the layer-3 Application structure, all the above three domain objects are located at the business logic layer. The entity domain objects represent the business data of the application in the memory, and the process domain objects are used to execute the business logic.
After object domain objects are created in the memory, they cannot exist forever. Therefore, they must be persisted. In a narrow sense, "persistence" only saves domain objects to the database permanently. in a broad sense, "persistence" includes various operations related to the database, such: save, update, delete, load, query, etc.
Iii. Comparison of common domain Object Persistence Technologies in Java
There are currently five common persistence modes in Java:
? Direct JDBC access to the database
? Active Domain Object Mode
? CMP Mode
? ORM Mode
? JDO Mode
1. Direct JDBC access to the database
Among these models, JDBC has a long history. Since the birth of Java, JDBC has been available and has now evolved to JDBC3.0. JDBC is a set of specifications, which specifies a unified standard interface. Each database vendor provides standard interface implementation. Therefore, you only need to master the standard SQL language to access different databases. The introduction of the JDBC specification announced to the world that there was a standard and common interface for accessing relational databases. Once the JDBC specification was released, it was an unprecedented success and soon became the standard for java database access. JDBC was supported by almost all database vendors. This kind of portability between databases is similar to that of Java's popular slogan Compile Once, Run everywhere. If Java is so elegant today, JDBC can be said to be an indispensable task. . When no JDBC is available, database vendors have their own APIs. It is very difficult for developers to access the database. Changing the database requires a great amount of modification for applications. Today, JDBC is the cornerstone of java's database access. The other modes discussed in this article only better encapsulate JDBC and provide more powerful interfaces at the upper layer.
JDBC implements database access by directly embedding SQL statements in business methods. SQL statements are relational-oriented and dependent on the relational model. Therefore, the advantage of the JDBC method is that it is simple and direct, especially for small applications.
However, JDBC brings the following disadvantages to applications:
(1) Code implementing business logic and database access code are mixed together to make the program structure unclear and less readable.
(2) Embedding relational SQL statements in program code makes it impossible for developers to fully write programs using object-oriented thinking.
(3) bind the business logic and the relational data model. If the relational data changes, you must manually modify all the relevant SQL statements in the Code, which makes it difficult to maintain the software.
(4) If the SQL statement in the program code contains a syntax error, this error cannot be checked during compilation. This error can only be found at runtime, which increases the difficulty of debugging the program.
It is precisely because of the shortcomings above that the following models have emerged in order to separate business logic from data access details.
2. Active Domain Object Mode
An active domain object is a form of entity domain object. It encapsulates the details of the relational data model and data access. 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 manages Persistence, namely, BMP (Bean-Managed Persistence), and 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 mode has the following advantages:
(1) encapsulate the data access details of the object in the entity domain. The process domain object is fully responsible for the business logic, so that the program structure is clearer.
(2) If the relational data mode changes, you only need to modify the code of the active domain object and do not need to modify the Business Method of the process domain object.
The active domain object mode has the following Disadvantages:
(1) The implementation of Object in entity domain still contains SQL statements.
(2) Each entity domain object is responsible for its own data access implementation. This function is divided into multiple objects, which leads to repeated data access operations on object domain objects, resulting in repeated encoding.
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.
3. 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.
The advantages of CMP mode are:
(1) He is based on the EJB technology and is the core part of SUN's J2EE system and has gained widespread support from the industry, including major manufacturers and open-source organizations. If you choose it for enterprise-level development, the technical support will be very complete.
(2) more and more functions, including comprehensive transaction support, EJBQL Query Language, and transparent distributed access.
The disadvantage of CMP mode is:
(1) entities developed by developers must comply with complex J2EE specifications, but there are no similar requirements for many ORM middleware.
(2) The entity domain EJB can only run in the EJB container, while the POJO can run in any Java environment.
(3) Despite the J2EE specification, EJB should be a portable component, which is indeed limited in actual application. This problem does not exist in ORM middleware.
4. ORM Mode
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. 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 ), including O/R, SQL self-generated, transaction processing, and Cache Management.
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.
Common ORM middleware include Hibernate, Apache OJB, Cayenne, Jaxor, and TopLink. 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. In addition, many of these designs are absorbed by J2EE standards organizations and become the latest EJB 3.0 standard.
5. JDO Mode
JDO is a new Data persistence technology in recent years. Java Data Objects (JDO) is a standard API developed by SUN to describe the persistence semantics of Objects. Strictly speaking,