Object Relational mapping ORM?

Source: Internet
Author: User
Tags inheritance microsoft sql server one table

Object Relational Mapping (relational Mapping, or ORM) is a technique to solve the mismatch between object-oriented and relational databases. In a nutshell, ORM automatically persists objects in a Java program to a relational database by using metadata that describes the mapping between the object and the database. Essentially, it's converting data from one form to another. This also implies additional execution overhead, however, if ORM is implemented as a middleware, there are many opportunities for optimizations that do not exist in the handwritten persistence layer. More importantly, the metadata needed to control the transformation needs to be provided and managed, but again, this is less expensive than maintaining handwriting, and even an object database that adheres to the ODMG specification still requires class-level metadata.

Object-Relational mapping (Object/relation Mapping, or ORM) is a result of the development of object-oriented software development methods. The object-oriented development method is the mainstream development method in the enterprise application development environment, and the relational database is the mainstream data storage system which is stored permanently in the enterprise-level application environment. object and relational data are two representations of business entities, and business entities behave as objects in memory and behave as relational data in the database. There are associations and inheritance relationships between objects in memory, and in a database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, object-relational mapping (ORM) system usually exists in the form of middleware, which mainly realizes the mapping of program object to relational database data.

Object-oriented is developed from the basic Principles of software engineering (such as coupling, aggregation, encapsulation), and the relational database is developed from the mathematical theory, and the two sets of theories have significant differences. In order to solve this mismatch, object-relational mapping technology emerged.

Let's start with the O/R. The letter O originates from "object", and R is derived from "relationship" (relational). In almost all programs, there are objects and relational databases. In the business logic layer and the user interface layer, we are object-oriented. When the object information changes, we need to keep the object's information in the relational database.

When you develop an application (do not use O/R Mapping), you may write a lot of data access layer code to save, delete, read object information from the database, and so on. You wrote a lot of ways to read object data, change state objects, and so on in the Dal. And the code is always repeating itself.

If you open your recent program and look at the DAL code, you will definitely see a lot of approximate generic patterns. As an example of how to save an object, you pass in an object, add SqlParameter to the SqlCommand object, correspond all the properties and objects, set the CommandText property of the SqlCommand to the stored procedure, and then run SqlCommand. Write the code repeatedly for each object.

In addition, there is a better way. There, introduce a O/R Mapping. Essentially, an O/R mapping will generate a DAL for you. Instead of writing the DAL code yourself, use the O/R Mapping. You use O/R mapping to save, delete, read objects, O/R mapping is responsible for generating SQL, you just need to care about the object just fine.

Object Relational mapping is successfully applied in different object-oriented persistence layer products, such as: Torque,ojb,hibernate,toplink,castor JDO, TJDO, etc.

The general ORM consists of the following four parts:
An API for CRUD operations on persistent class objects;
A language or API used to specify queries related to class and class attributes;
A tool that prescribes mapping metadata;
One technique allows ORM implementations to dirty checking with transactional objects, lazy association fetching, and other optimizations.

One, the current popular ORM products

At present, many vendors and open source communities have provided the implementation of a durable 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, and others are open source projects.

Hibernate's lightweight ORM model gradually establishes the leading position in the Java ORM Architecture, and even replaces the complex and cumbersome EJB model as the de facto Java ORM Industry standard. And many of these designs have been absorbed by the Java EE Standards Organization and become the standard of the latest EJB 3.0 specification, which is also a strong testimony of the open source project affecting industry standards.

Two, object-relational mapping mode

From the "public warehouse meta-model: Development Guide," the 8th chapter of the CWM meta-warehouse extracts from the content, the implementation of the public warehouse meta-model (CWM) UML diagram to the Microsoft SQL Server database mapping, is a way to map the object hierarchy into a relational structure. Personally, it can be used as a reference method to store ontology (Ontology) files in relational database.

Basic situation: The public warehouse meta-model (CWM) is an object management organization (OMG) and data Warehouse-related meta-model standards, the use of UML-represented object hierarchy, when saved to the database due to the imperfect object-oriented database technology (theoretical research and business applications are not mainstream), So the author of the book tends to use a mature relational database to save-this is also the problem of storing the ontology.

Adoption method: The UML model of the various elements through the transformation, save as a database schema. Because Cwm is a meta-model, an instance of the model is also a model that saves this instance as database data. Improve development and execution efficiency with more mature stored procedure technologies in the database.

1. Data type mapping mode

1.1 Simple data type pattern: build a mapping table of simple data types in UML and relational databases to guide mapping.
1.2 Enumeration data type pattern: One table for each enumeration type, and only one column (_enumliteral) represents the enumeration value.
1.3 Class-based data type pattern: Use a FOREIGN KEY constraint to associate the underlying column with a class-based type instance.

2. Class Mapping model

Each class corresponds to a table. Single-valued, multivalued, and inherited relationships can be mapped in the following ways, whereas reference properties are mentioned in the associative mapping pattern.

2.1 Single-valued attribute mode: A property with an upper bound of cardinality of 1, mapped to the column of the table corresponding to the class. If the lower bound is also 1 (Required property), the column property is not NULL.
2.2 Multivalued attribute pattern: Each multivalued attribute is mapped to a separate table, and the foreign key is used to connect to the table corresponding to the class.
2.3 Inheritance mode: Every time an instance of a class is added, the object of each class is generated from top to bottom according to its inheritance, and the objects have the same ID (the primary key of the corresponding record for the root object). When you delete an object instance, the data is deleted from the bottom up. What to do if you encounter a deletion from the middle. How multiple inheritance is handled. (Golden Dragon Fly)

3. Correlation mapping mode

3.1 A-to-one correlation mode: Adds a column at both ends of the association.
3.2 One-to-many correlation modes: Same as 3.1. If more of this end is ordered, you also need to add a column to indicate the ordinal.
3.3 Many-to-many correlation patterns: separate the associations into a single table.
3.4 Combined correlation mode: note cascading deletes.
3.5 Inversion Correlation Pattern: The association ends up pointing to the related type, as is the normal association.
3.6 Paired Correlation Patterns: Association records the relationship between two classes, represents an association with an intersection class, represents a single table, each association corresponds to a table, and a foreign key represents the relationship between them.
The OCL on the 3.7 association needs to be parsed into the corresponding stored procedure code.
3.8 Ensure that the associated cardinality also needs to be parsed into the corresponding stored procedure code.

4. Reference mapping mode


A MOF feature that does not exist in UML, which refers to an instance of a property declared as a reference type. Implemented with stored procedures.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.