What is ORM?

Source: Internet
Author: User

Object/Relation Mapping

 

Object-relational database ing

What is ORM?

Object relationship ing (ORM) provides a conceptual and easy-to-understand Method for modeling data. The ORM methodology is based on three core principles:

Simple: data is modeled in the most basic form.

Communication: the database structure is documented in a language that anyone can understand.

Accuracy: Create a correctly standardized structure based on the data model.

Typically, modelers develop information models by collecting information from those who are familiar with the application but are not skilled in data modelers. Modelers must be able to communicate with data structures at the conceptual level with terms that non-technical enterprise experts can understand. Modelers must also be able to process sample data with simple unit analysis information. ORM is specially designed to improve this connection.

Rule expression

ORM represents the application world as a group of objects (entities or values) with roles (LINKS ). ORM is also known as fact-based modeling because it describes relevant data as basic facts. If these facts are separated into smaller ones, information will be lost.

Examples of simple facts include:

People have phone

People live somewhere

Life on a certain date

A person is hired on a certain date

ORM not only provides a simple and direct way to describe the relationship between different objects. ORM also provides flexibility. Models created Using ORM are more capable of adapting to system changes than models created using other methods. In addition, ORM allows non-technical enterprise experts to talk about models based on sample data, so they can use real-world data verification models. Because ORM allows reuse of objects, the data model can be automatically mapped to a correctly standardized database structure.

The simplicity of the ORM model simplifies the database query process. With the ORM query tool, you can access the expected data without understanding the underlying structure of the database.

========================================================== ========================================================== ========================

Object Relational ing (ORM) is a technology designed to solve the mismatch between objects and relational databases. To put it simply, Orm automatically persists the objects in Java programs to relational databases by using metadata describing the ing between objects and databases. In essence, data is converted from one form to another. This also implies additional execution overhead. However, if ORM is implemented as a middleware, there will be many opportunities for optimization, which do not exist in the handwritten persistence layer. More importantly, the metadata used to control the conversion needs to be provided and managed; however, these costs are less than the manual maintenance solution; in addition, even object databases that comply with the ODMG specifications still need class-level metadata.

Object/Relation Mapping (ORM) is generated with the development of object-oriented software development methods. Object-Oriented development is the mainstream development method in today's enterprise-level application development environment. Relational databases are the mainstream data storage systems that permanently store data in enterprise-level application environments. Objects and relational data are two forms of business entities. business entities are represented as objects in the memory and relational data in the database. Objects in the memory have associations and inheritance relationships. In the database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, the object-relational ing (ORM) system generally exists in the form of middleware, mainly to map program objects to relational database data.

Object-Oriented development is based on the basic principles of Software Engineering (such as coupling, aggregation, and encapsulation), while relational databases are developed from mathematical theories, there are significant differences between the two sets of theories. To solve this mismatch, the object link ing technology emerged.

Let's start with O/R. The letter O originated from "object", while R came from "relational ). Objects and relational databases exist in almost all programs. In the business logic layer and user interface layer, We are object-oriented. When the object information changes, we need to save the object information in the relational database.

When you develop an application (without O/R Mapping), you may write a lot of data access layer code to save, delete, and read object information from the database. You have written many methods in the DAL to read object data, change State objects, and so on. These codes are always repeated.

If you open your recent program and look at the DAL code, you will certainly see many similar general patterns. Let's take the method of saving objects as an example. You pass in an object, add SqlParameter to the SqlCommand object, map all attributes to the object, set the CommandText attribute of SqlCommand as the stored procedure, and then run SqlCommand. This code must be repeatedly written for each object.

In addition, is there a better way? Yes. Introduce an O/R Mapping. Essentially, an O/R Mapping will generate a DAL for you. It is better to use O/R Mapping than to write the DAL code by yourself. You use O/R Mapping to save, delete, and read objects. O/R Mapping is responsible for generating SQL statements. You only need to care about objects.

Object relationship ing is successfully applied to different object-oriented persistence layer products, such as Torque, OJB, Hibernate, TopLink, Castor JDO, and TJDO.

The general ORM includes the following four parts:

An API for performing CRUD operations on persistent class objects;

A language or API is used to specify queries related to classes and class attributes;

A tool that specifies mapping metadata;

One technology allows the implementation of ORM to perform dirty checking, lazy association fetching, and other optimization operations together with the transaction object.

I. currently popular ORM Products

At present, many manufacturers and open-source communities have provided implementation of the persistent layer framework. Common implementations include:

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.

Ii. Object-link ing mode

The contents extracted from Chapter 8th of "public warehouse meta-model: Development Guide" are used to implement the public warehouse meta-model (CWM) UML ing from a UML diagram to a Microsoft SQL Server database is a way to map object hierarchies into a relational structure. I personally think it can be used as a reference to store Ontology files in relational databases.

Basic information: the common warehouse metadata model (CWM) is an object management organization (OMG) metadata model standard related to the Data Warehouse. It adopts the object hierarchy represented by UML, because of the imperfect Object-Oriented Database Technology (neither theoretical nor commercial applications are mainstream ), therefore, the author of this book tends to use mature relational databases for storage-this is also a problem encountered when storing the ontology.

Usage method: convert various elements in the UML Model and save them as the database mode. Because CWM is a metadata model, the instance of the model is also a model that stores such instances as database data. Improve development and execution efficiency by using mature stored procedures in databases.

1. Data Type ing mode

1.1 simple data type mode: Create a ing table for simple data types in UML and relational databases to guide ing.

1.2 enumeration data type mode: Each Enumeration type corresponds to a table, and only one column (_ EnumLiteral) represents the enumeration value.

1.3 class-based data type mode: use foreign key constraints to associate basic columns with class-based type instances.

2. Class ing model

Each class corresponds to a table. Single-value attributes, multi-value attributes, and inheritance relationships can be mapped using the following methods, and referenced attributes are described in the associated ing mode.

2.1 single-Value Attribute mode: it is the attribute with the upper bound of cardinality 1, which is mapped to the column of the table corresponding to the class. If its lower bound is also 1 (mandatory), the column attribute is not null.

2.2 multi-value attribute mode: Each multi-value attribute is mapped to an independent table, and a foreign key is used to connect to the table corresponding to the class.

2.3 inheritance mode: When a class instance is added, the class objects are generated from top to bottom based on their inheritance relationships. These objects have the same ID (the primary key of the root object corresponding to the record ). When deleting an object instance, data is deleted from the bottom up. What should I do if I delete data from the center? How to deal with multi-inheritance? (Jin longfei)

3. Associated ing mode

3.1 one-to-one association mode: Add a column to each end of the association.

3.2 One-to-Multiple Association mode: Same as 3.1. If multiple ends are ordered, you must add a column to indicate the sequence number.

3.3 Multi-to-Multi-join mode: Separate the join table.

3.4 combined Association mode: Pay attention to cascading deletion.

3.5 reverse Association mode: The types at the two ends of the Association point to the correlation type, which is the same as the common association.

3.6 paired Association mode: The Association records the relationship between two classes, and uses the intersection class to indicate Association, representing a separate table. Each association corresponds to a table, foreign keys are used to represent the relationships between them.

3.7 The associated OCL needs to be analyzed into the corresponding stored procedure code.

3.8 Ensure that the associated cardinality needs to be analyzed into the corresponding stored procedure code.

4. Reference ing mode

A mof feature that does not exist in UML is an instance declared as a reference type. Use 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.