Save the Association as a table and store the foreign key of the associated table
- In an object, a collection is used as a domain value to handle multiple domains.
- In db, there can be only a single domain value.
- The core of the foreign key mapping is the use of foreign keys to maintain contact on the single-valued side of the association relationship.
- In many-to-many relationships, there is no single-valued end.
- Operating mechanism
- Use a linked table to save the association relationship.
- There are only two fields, that is, the foreign key IDs of the two associated tables.
- For each pair of associated objects, it will have a row of data corresponding to it.
- The linked table does not have a corresponding memory object. So there is no ID. Its primary key is a combination of two primary keys for the associated table.
-
- Loading data from a linked table requires two queries, such as the problem of loading a function for an employee
- 1, Query the Employeesskills table and draw all the rows that the employee is associated with.
- 2, based on the ID of these associated rows, find out all the function objects.
- When this information already exists in memory, there is no problem. Otherwise, the DB query overhead is required multiple times.
- Use time
- The standard situation is a many-to-many association relationship.
- When the associated table structure cannot be changed, it can only be used.
- Sometimes the associated table stores information about a relationship, and it corresponds to a real domain object.