Mappings of inheritance relationships in memory and in DB

Source: Internet
Author: User
Tags table definition

  • Use
      • Map a number of similar classes to a single table, using specific table inheritance for classes that have many special data.
      • Use class table inheritance for high levels, and use specific tables for low-level inheritance.
  • single table inheritance
    • in db, the class inheritance hierarchy is designed as one table, and the columns in the table represent all the fields in the different classes.

    • run mechanism
      • each class is responsible for saving the data associated with it in one row of the table. Other unrelated columns in the table are left blank.
      • when you decide to load an object into memory by using the Type field in the table, you should instantiate that class to create the object. The
        • can use the class name
        • directly or the code domain that needs to be translated.
      • The code that holds the data can be owned by the layer superclass.
    • Use time
      • benefits
        • You need only focus on one DB table.
        • The
        • does not have to be connected when getting data.
        • Refactoring of inheritance hierarchies (moving a domain between parent and child) does not require a modification of the db.
      • Disadvantage
        • The columns of the DB table and the object's fields are not one by one corresponding.
        • waste of space.
        • The
        • may be too large for a single table. Too many indexes are locked frequently. resulting in inefficient access.
        • domain name may be duplicated (plus the class name can be resolved).
  • class Table Inheritance
    • represents a class for each db.
    • The fields in the

    • run mechanism
      • class are mapped directly to the fields in the corresponding table.
      • row link problem for table
        • use a common primary key. Because the superclass table has a row for each row in the table for the other subclasses. The primary key must be unique between the tables.
        • each table has its own primary key and uses the foreign keys of the superclass table to link the rows together.
        • The biggest problem with
      • is how to effectively retrieve data from multiple tables. The
        • makes one call to each table, which causes multiple calls.
        • one-time link to work with multiple tables can produce performance problems when the number of tables is large.
      • Generally, for a specified query, it is not clear that you need to link those tables. such as querying a group of athletes.
        • uses external links to effectively link some tables when they do not have data.
        • The
        • reads the root table first, and then finds the next table to read. However, there will be multiple queries.
    • use time
      • benefits
        • db tables are easier to understand and have no wasted space.
        • The relationship between the
        • domain model and DB is straightforward.
      • defect
        • Loading an object requires access to multiple tables (that is, links required) The
        • field moves up or down in the inheritance hierarchy, resulting in db refactoring.
        • frequent access to a superclass can cause bottlenecks.
  • Concrete Table Inheritance
    • Each specific class in memory corresponds to a DB table.

      • Operating mechanism
        • The columns in the DB table contain all the fields in the specific class and all of its ancestor classes. All fields in the superclass are copied in the DB table of the subclass.
        • All table keys in the inheritance hierarchy need to be guaranteed to be unique.
          • A key distribution system is required to record the usage of keys between tables.
          • Either avoid using the superclass's domain or use a composite key that contains the table ID.
        • Referential integrity issues
          • For example, a charity activity is associated with a player. There is no DB table for the player. Therefore, the foreign key cannot be used to construct the linked table.
          • At this point, referential integrity can be ignored.
          • Or, use a multi-link table. Each table in the DB has a linked table corresponding to it.
      • Use time
        • Advantages
          • Each DB table is self-contained and does not contain unrelated domains.
          • A link operation is not required when the specific mapper reads data.
          • The corresponding DB table is only accessed when the class is accessed. The access load is distributed.
        • Defects
          • The primary key is difficult to handle.
          • Database relationships cannot be added to abstract classes.
          • The table definition must be changed when the domain moves on the inheritance tree.
          • When the domain of the superclass changes, there will be a lot of sub-class DB tables associated with the changes.
          • A lookup on a superclass needs to check all tables.
  • inheritance Mapper
    • run mechanism
      • Find method definition on a specific subclass, because it returns a concrete class. OO does not allow changing the return value type of a declared method.
        • Basic behavior: Locate the appropriate row in the DB, instantiate an object of the correct type (subclass decision), and load the object with data from DB.
        • The
        • subclass Mapper, after loading the subclass-specific data, has been raising its superclass method.
        • The
      • Insert and update
        • uses the Save method. You can define an interface on a superclass.
        • Insert: Creates a new row and saves the data in the object with the Save Hook method.
        • Update: Value to save data, also use the Save Hook method.
        • similar to load, each class holds its own unique data, and then calls the Save method of the superclass.
      • An abstract class that supports saving and loading
        • the abstract player mapper is responsible for loading and saving specific player data to the DB.
          • It is an abstract class. Its behavior is used only by specific mapper subclasses.
        • standalone player mapper. The
          • provides an interface for the operation.
          • The
          • provides a lookup method.
          • The
          • overrides the insert and update methods.
          • The
          • is only responsible for locating the specific mapper that should handle a specific task and delegating the task to that specific mapper.

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.