What is learning notes persistence and Object Relational mapping ORM Technology
by Naven at 2005-09-19
What is "persistence"
Persistent (persistence), that is, to save data (such as an object in memory) to a storage device that can be permanently saved, such as a disk. The main application of persistence is to store the data in memory in a relational database, but it can also be stored in a disk file, in an XML data file, and so on.
What is "persistence layer"
Persistence layer (persistence Layer), which is a logical aspect of a particular system that focuses on implementing data persistence applications, associates data consumers with data entities.
What is "Object Data Map (ORM)"
Orm-object/relational Mapper, or "object-relational data mapping component". For O/R, which is object and relational (relational data), it means that both object-oriented and relational data must be used for development.
Note: ORM in the modeling field is Object/role modeling (object Role modeling). In addition, this is "O ' Mapper" instead of "O/R Mapping". In contrast, the O/R Mapping describes a design idea or implementation mechanism, while the O/R mapper refers to the persistence Framework (framework) designed with the O/R principle, including the O/R mechanism, as well as SQL generation, transaction processing, cache management, and so on.
In addition to ORM technology, there are several persistence technologies
Active Domain object mode
It is a form of encapsulating relational data models and data access details in the implementation. In the Java EE Architecture, the EJB component is divided into session EJB and entity EJB. Session EJBS typically implement business logic, while entity EJBS represent business entities. Entity EJBs are divided into two types: the EJB itself manages persistence, i.e. BMP (bean-managed persistence), and EJB container management persistence, or CMP (container-managed persistence). BM P is an example of active domain object mode, BMP represents the management of data access details by the entity EJB itself.
The active domain object itself is located in the business logic layer, so when the active domain object mode is used, the whole 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 semantic persistence of objects. JDO, strictly speaking, is not an object-relational mapping interface because it supports the persistence of objects into any kind of storage system, including relational databases, object-oriented databases, xml-based databases, and other proprietary storage systems. Because relational databases are currently the most popular storage systems, many implementations of JDO contain object-relational mapping services.
CMP mode
In the Java EE Architecture, CMP (container-managed persistence) represents the persistence of entity EJBs managed by the EJB container, which encapsulates the mapping of object-relationship and data access details. The similarity between CMP and ORM is that both provide object-relational mapping services, separating the task persistence tasks from the business logic. The difference is that CMP is responsible for persisting entity EJB components, and ORM is responsible for persisting POJO, which is an ordinary Java Bean-based entity domain object.
An entity domain object based on Java Bean is generally referred to as POJO (Plain old Java object), meaning the common and ancient Java objects. With the sophistication and popularity of various ORM mapping tools, POJO, which is simple and highly portable compared to the CMP-based entity EJB, the combination of ORM mapping tools and POJO has become an increasingly popular alternative to CMP's persistence scheme. The disadvantage of POJO is that it is impossible to make remote calls and does not support distributed computing.
Why do persistence and ORM design
In the current enterprise application system design, MVC, that is model (models)-View (view)-control is the main system architecture pattern. The Model in MVC includes complex business logic and data logic, as well as data access mechanisms such as JDBC connections, SQL generation and statement creation, and read resultset result sets. The separation of these complex business logic and data logic to transform the tight coupling relationship into loose coupling relation (i.e., decoupling) is an urgent task to reduce the coupling degree of the system, and also to do the work of persistence. The MVC pattern implements the decoupled decoupling of the presentation layer (view) and the data Processing layer (model), while the persistent design realizes the decoupling of the business logic and the data logic separation within the processing layer. ORM, as the most important and complex technology in the persistent design, is also the hot technology in the industry at present.
In simple terms, business process logic and data access logic are mixed together by the usual system design, using the JDBC operations database.
Generally basic are the following several steps:
1, establish the database connection, obtains the Connection object.
2, according to the user Input assembly query SQL statements.
3, according to the SQL statement to establish Statement objects or PreparedStatement objects.
4, execute the SQL statement with the Connection object, obtain the result set ResultSet object.
5, then one by one read the result set ResultSet the data in the object.
6, according to the data read, according to the specific business logic to calculate.
7, according to the results of the calculation of the assembly to update the SQL statement.
8. Then use the Connection object to execute the update SQL statement to update the data in the database.
7. Finally, close each Statement object and Connection object in turn.
It can be seen from the above that the code logic is very complex, which does not include the processing logic that a statement fails to perform. The business process logic and the data access logic are all mixed together. While a complete system contains tens of thousands of such repetitive and mixed processes, the amount of code to be changed is unthinkable if some of these business logic or some associated business processes are to be modified. On the other hand, it could be an impossible task to change the database product or the operating environment. And the user's operating environment and requirements are very different, we can not for each user in each of the operating environment to design a set of the same system.
So it is necessary to separate the same processing code, the business logic and possibly the different processing, the data access logic, on the other hand, the data in the relational database are basically accessed by a row of data, while the program is processed by a single object, and most of the current database-driven technologies (such as Ado.net, JDBC, ODBC, and so on) are all processed in the result set of a rowset. So in order to solve this problem, there is an ORM object and the mapping technology between the data.
For example, for example, to complete a shopping discount promotion program, the ORM idea will be implemented as follows (quoted in "Simple Hibernate"):
The business logic is as follows:
Public Double calcamount (String customerid , double amount
{
//customer record is obtained from customer ID
Customer customer = Customermanager. GetCustomer (Custmerid);
//Get discount rules based on customer rating
Promotion promotion = Promotionmanager.getpromotion ( Customer.getlevel ());
//Cumulative total customer consumption and save cumulative results
customer.setsumamount (customer.getsumamount). Add (amount);
Customermanager.save (customer);
//Returns the amount after the discount
return amount.multiply (Protomtion.getratio ());
}
so that the code is very clear and completely separate from the data access logic. When designing business logic code, there is no need to consider the same operation of database JDBC, and give it to Customermanager and Promotionmanager two classes to complete. This is a simple ORM design, and the actual ORM implementation Framework is much more complex than this one.
What are the current popular ORM products
At present, many vendors and open source community have provided the implementation of the persistence layer framework, common
Apache OJB (http://db.apache.org/ojb/)
Cayenne (http://objectstyle.org/cayenne/)
Jaxor (http://jaxor.sourceforge.net)
Hibernate (http://www.hibernate.org)
IBatis (http://www.ibatis.com)
Jrelationalframework (http://ijf.sourceforge.net)
Mirage (Http://itor.cq2.org/en/oss/mirage/toon)
Smyle (Http://www.drjava.de/smyle)
TopLink (http://otn.oracle.com/products/ias/toplink/index.html)
TopLink is Oracle's commercial product, others are open source projects.
Among them, Hibernate's lightweight ORM model has gradually established the leading position in Java ORM Architecture, and even replaced the complex and cumbersome EJB model as the de facto Java ORM Industry standard. And many of these designs have been incorporated into the standard of the latest EJB 3.0 specification by the EE Standards Organization, which is also a powerful testimony of the impact of open source projects on industry standards.
References: 1, "Simple Hibernate"
2, "Proficient in Hibernate:java object persistence technology detailed"