C + + ORM ODB Introduction (ii)

Source: Internet
Author: User

Catalogue [-]

    • 1. Types of inheritance in ODB
    • 2. The difference between abstract and polymorphic
    • 3.polymorphic table
    • 4. How the previous version implemented polymorphic
    • 5. Example

This section focuses on the relationship between inheritance in ODB and C + +.

The relationship of O in ORM is very complex. One of them is the inheritance system of objects.

Supports inheritance between classes in the C + + language, can these inherited relationships be mapped to the database at the same time?

1. Types of inheritance in ODB

ODB supports inheritance types in 2, abstract and polymorphic. Based on the description of the ODB document, the abstract method is multiplexed inheritance. Polymorphic is a virtual inheritance (a dynamic union that supports C + +).

We know that to define a C + + class as a database object, you need to tell the ODB compiler using the #pragma DB object directive. Both abstract and polymorphic are a pattern of object, so you need to use the instruction #pragma db object abstract or #pragma DB object polymorphic directive to inform the compiler of the inheritance model of the base class.

Abstract and polymorphic cannot coexist in the same base class, or choose abstract or choose Polymorphic.

2. The difference between abstract and polymorphic

Judging from the performance of the database, abstract corresponds to the object class, ODB did not generate a corresponding table for it, it is surprising, not that each object class will at least map a table? How to abstract object without it? Because the derived class simply merges the fields of the abstract class into its own table, it's a bit of a C + + pod type, just a combination of memory layouts, but here it's merged on the same table.

In addition, abstract object cannot have a field marked as an id attribute. Because he is not a database persistence object. It is also not possible to manipulate abstract object classes directly using database methods such as load, persist, update, find, erase, and so on.

The behavior of polymorphic is almost the same as the dynamic type of C + +. ODB will generate a separate table for the base class of polymorphic, which will contain the type information of the subclass. Polymorphic can have an id attribute, the ID of the entire inheritance system on the same linear space, i.e. the IDs of objects of different derived classes cannot be the same. At load, find, the correct subclass is automatically created based on the class type stored in the database table. In the persist, update, erase, can also be directly used only polymorphic base class call, ODB generated code will automatically identify the subclass of the type, and call the corresponding trait class.

3.polymorphic table

ODB Document Description: polymorphic tables are available in three different ways, Table-per-hierarchy, table-per-difference, and Table-per-class

Table-per-hierarchy: It is a common table for each polymorphic class and its derived classes. A large table, with null for unrelated field assignments.

Table-per-difference: That is, the polymorphic base class has a table where each derived class has its own table, and the derived class's table holds only the fields that resemble itself, not the fields of the base class. However, there is a problem with the load object that requires a federated query or 2 queries. 2 or more SQL statements are required for update,persist. (Recommended, database does not have too many redundant fields)

Table-per-class: Each derived class is a table, and the information for the base class is merged together. Similar to the table for derived classes in abstract.

4. How the previous version implemented polymorphic

The polymorphic feature was introduced only in the ODB1.8 version. Early 1.4, version 1.5 only supports the abstract mode. At this time ODB itself does not support dynamic types, it is necessary to add an additional association mapping object, in addition to the class name, the field and the abstract class exactly the same, and add their own type information field. In the dbevent persist, erase event, synchronous operation of the association class, relatively troublesome.

5. Example

Data model for getting started with (a);

Add the type of database ID 1 to a zone class. Then the 2 method can correctly load zone information from the database.

Shared_ptr<zone> z = db.load<zone> (1);

Shared_ptr<entity> e = db.load<entity> (1);

Persist, shared_ptr<zone> Z (New Zone ()) or shared_ptr<entity> (new Zone ()) can be called directly db.persist (z). Update, erase and persist are the same.

Operations such as Persist\update\erase cannot be called on the abstract class.

Share to:0 likes Disclaimer: Oschina Blog article copyright belongs to the author, protected by law. Not reproduced without the consent of the author.

C + + ORM ODB Introduction (ii)

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.