hibernate notes 3

Notes 3

1. Table relationships in the database

One-to-one, one-to-many (many-to-one), many-to-one

2. How to establish the table relationship in the table

How to implement a one-to-many relationship: using foreign key constraints, the one side is called the master table, and the many side is called the slave table.

Foreign key: There is a column in the slave table. Except for null, the value of this column can only be derived from the primary key of the main table. By default, the value of the foreign key field can be repeated.

How to implement a many-to-many table relationship?

Using the intermediate table, the intermediate table can only have two foreign keys, refer to the primary keys of two many-to-many tables, and cannot have other field information, and the primary key of the intermediate table adopts the joint primary key

If any multi-party table is compared with the intermediate table, it is a one-to-many relationship.

How is a one-to-one table relationship implemented in a database? two

1. How to create a foreign key:

Using foreign key constraints, unique constraints, and non-null constraints, he adds unique constraints and non-null constraints to foreign key constraints to achieve one-to-one.

2. How to use the primary key:

Make one of the tables both primary and foreign keys

How to determine the relationship between two tables: find foreign keys

3. Steps to follow for multi-table mapping

Step 1: Establish the relationship between the two tables

Step 2: Create a many-to-many table relationship in the database

Step 3: Describe the relationship between the two entity classes in the entity class

Step 4: Establish the relationship between the two tables and the two entities in the mapping configuration

4. One-to-many relationship mapping configuration and its operation

Example: Two tables of customers and contacts

Step 1: Establish the relationship between the two tables

  A customer can contain multiple contacts, and multiple contacts can belong to the same customer, so there is a many-to-many relationship between customers and contacts

Step 2: Create a many-to-many table relationship in the database

  The realization of one-to-many relationship relies on foreign keys, the customer is the master table, and the contact is the slave table, you need to add a foreign key to the contact table

Step 3: Describe the relationship between the two entity classes in the entity class

  The entity class of the main table contains a collection reference from the entity class of the table

  The entity class of the slave table should contain the object reference of the entity class of the master table

Step 4: Establish the relationship between the two tables and the two entities in the mapping configuration

5. Many-to-many relationship mapping configuration and its operation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895853&siteId=291194637
Recommended