Are you O/R Mapping? [Reprint]

Source: Internet
Author: User
Are you O/R Mapping?

In today's software development, the phrase "O/R Mapping" is increasingly mentioned. What is o/R Mapping? In short, object/Relation Mapping maps objects to links. This name and concept come out there? Why do we need o/R Mapping during development?

First, let's look at object-oriented development. Object-oriented development has been widely used in recent years and has become the most basic method for software development. The development language also has a special programming language for object-oriented development. For example, the mainstream Java, C # Are all programming languages based on object-oriented development. Practice has proved that object-oriented is a development method that can effectively improve the development quality and be easy to manage and conform to human thinking patterns.

Whether it is a process/structure oriented development method or an object oriented development method, almost all projects and product software development must involve issues such as data access updates of relational databases. Dealing with relational databases is the first step in product development for all projects. Currently, the development technology needs to develop a data access layer for each project) as the basis of business logic code.

In object-oriented development, the most basic database operations at the data access layer include inserting, deleting, updating, and querying objects. Almost all project development requires the implementation of these atomic operations first. These operations are similar in almost all project development. They convert the insert object, delete object, update object, and query object-level operations into the insert statements supported by the underlying relational data, delete, update, select, and other SQL operations.

Let's look at a basic example to insert an object. Here we add a user object account.

public void addAccount(final Account account) throws DAOException,            AccountAlreadyExistException {     final Connection conn;     conn = getConnection();     final PreparedStatement stat                    = conn.prepareStatement("insert into "                    + PersistentAccount.PROPERTY_ACCOUNT_TABLE                    + " ( "                    + PersistentAccount.PROPERTY_USER_NAME + ","                    + PersistentAccount.PROPERTY_PASSWORD + ","                    + PersistentAccount.PROPERTY_EMAIL + ","                    + PersistentAccount.PROPERTY_COLUMN_WRITER + ")"                    + " values( '"                    + account.getUserName() + "', '"                    + account.getPassword() + "', '"                    + account.getEmail() + "', '"                    + (account.isColumnWriter() ? "Y" : "N") + "')");     stat.execute();     stat.close();     conn.close();     return; }

This operation is very simple. It is to convert the object logic of adding an account object into an insert operation, that is, to map the simplest account object to the account of the database table. In this example, developers manually write JDBC code. If there is another object person, we also need to write similar code to add a person operation.

Similar examples and codes exist in many projects. From JDBC in 1996 to the present eight years, a large number of project development is still writing such code repeatedly to achieve the basic operation required for each of these projects. The development data access layer requires a lot of work (development and testing) and requires developers. Unfortunately, in a large number of project development projects, we need to repeat this complex data access layer development, as a result, a large amount of human, material, and financial resources are wasted in the repeated development of this human-intensive data access layer, and database operations are precisely the most error-prone and time-consuming part of project development. If the project needs to face different databases, you also need to develop a dedicated data access layer for each database.

Obviously, this is a significant waste of code that repeats every project but has the same pattern. This gave birth to the O/R Mapping technology. Orm, that is, object-relationl mapping, is used for automatic ing between relational databases and objects. In this way, when we operate a database, the o/R Mapping Tool automatically converts object operations to SQL statements without dealing with complex SQL statements. In this way, we only need to focus on the object architecture in the business logic, rather than the underlying repetitive Database SQL and JDBC code.

Let's take a look at the differences between the original code and the O/R Mapping technology. Here we use JDO.
 

public void addAccount(final Account account) throws DAOException,            AccountAlreadyExistException {    final PersistenceManager pm = pmf.getPersistenceManager();    pm.currentTransaction().begin();    pm.makePersistent(account);    pm.currentTransaction().commit();
}

After comparison, we can see the huge difference in the amount of code. This is just the simplest way to insert a single object.

When inserting multiple objects, especially multiple associated objects, you can see the advantages of O/R Mapping. In the Liberator JDO framework, persistence by reachability is supported. When saving an object, all objects referenced by this object can be automatically saved to the corresponding data table, you do not need to save every object.
After comparison, we can see the huge difference in the amount of code. This is just the simplest way to insert a single object.

When inserting multiple objects, especially multiple associated objects, you can see the advantages of O/R Mapping. In the Liberator JDO framework, persistence by reachability is supported. When saving an object, all objects referenced by this object can be automatically saved to the corresponding data table, you do not need to save every object.

The following example stores a department, which contains all the student objects and each student's profile (profiel ).

Final persistencemanager PM = PMF. getpersistencemanager ();
Final profile myprofile1 = new profile (180, "foo1", "foolast1", 19 );
Final student student1 = new student (1, myprofile1 );
Final profile myprofile2 = new profile (181, "foo2", "foolast2", 19 );
Final student student2 = new student (2, myprofile2 );
Final Department = new Department ("computer ");
Department. addstudent (student1 );
Department. addstudent (student2 );
PM. currenttransaction (). Begin ();
PM. makepersistent (Department );
PM. currenttransaction (). Commit ();

If the traditional manual SQL and JDBC code are used, the code size can be nearly five times larger. The computing test saves time. The o/R Mapping technology can greatly improve the development efficiency and time, and the Development quality is more easily guaranteed.

Here we only show a small part of the O/R Mapping technology, and there are more attractive features worth trying.

Benefits of using o/R Mapping:
1) improve learning and development efficiency and greatly reduce development costs.
Using ORM can greatly reduce learning and development costs, and the development of modern technology makes us need to continue learning. We should not only learn object-oriented, UML, design patterns, but also the knowledge of SQL, JDBC, and even various databases (DB2, Oracle, SQL Server, etc. In actual development, what really benefits customers is their unique business functions. The current situation is that we spend a lot of time writing data access, it also takes a considerable amount of time to process bugs, such as bug search and maintenance. That is to say, in actual development, a lot of time is wasted on non-business events that do not create value at all.

After using Orm, we do not need to waste too much time on SQL statements and JDBC development and testing. The ORM framework has transformed underlying database operations into familiar object operations. We only need to understand object-oriented development to develop database applications.

2) Simplify the code and reduce the number of bugs.
By establishing an ORM framework, code development can be greatly reduced, and the data layer development is relatively simple, greatly reducing the chance of errors.

3) simplified testing. You only need to test the class and behavior of the business logic to avoid repeated JDBC tests.

4) Improving the Performance of Object-level caching (Cache objects and their relationships) can avoid unnecessary database access and greatly improve data read/write performance. We can also save time and effort for system optimization.

5) isolate the data source and easily convert the database ORM to separate the business layer from the actual data storage. Developers do not need to care about the actual storage mode, if you need to replace the SQL Server database with an Oracle database, you only need to modify the configuration file, and the business logic Code does not need to be modified at all.

The great superiority of O/R Mapping technology makes it quickly a mainstream development mode in object-oriented development. In the Java Community, Java data object is a Java o/R Mapping standard jointly developed by many manufacturers, and the next generation of Enterprise Java Bean standards will also integrate JDO technology, the o/R Mapping technology is the core technology of EJB 3.0 technical specifications. Microsoft is not weak in that it provides an O/R Mapping Framework called objectspaces in the next-generation. NET development framework. It is foreseeable that in the near future, O/R Mapping technology will be widely used in software project development and become a rocket booster for software development.

 

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.