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
<!-- One-to-many main table entity configuration Label Set Used to configure set collection properties Attributes: name: Specifies the attribute name of the set collection in the entity class table: specify the name of the slave table, in the one-to-many configuration, you can not write key Role: used to map foreign key fields Attributes: column: Specify the foreign key field name one-to-many: Role: used to establish a one-to-many mapping configuration Attributes: class: used to specify the name of the entity from the table --> <set name="linkmans" table="cust_linkman"> <key column="lkm_cust_id" ></key> <one-to-many class="LinkMan"></one-to-many> </set>
The entity class of the slave table should contain the object reference of the entity class of the master table
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wbs.domain"> <class name="LinkMan" table="cust_linkman"> <id name="lkmId" column="lkm_id"> <generator class="native"></generator> </id> <property name="lkmName" column="cust_lkmName"></property> <property name="lkmGender" column="cust_lkmGender"></property> <property name="lkmPhone" column="cust_lkmPhone"></property> <property name="lkmMobile" column="cust_lkmMobile"></property> <property name="lkmEmail" column="cust_lkmEmail"></property> <property name="lkmPosition" column="cust_lkmPosition"></property> <property name="lkmMemo" column="cust_lkmMemo"></property> <!-- One-to-many relationship mapping: mapping configuration from table entities --> <!-- many-to-one establishes a one-to-many mapping configuration Attributes: name refers to the name of the entity object reference from the table entity class specifies the entity class name corresponding to this attribute column specifies the name of the foreign key field from the table --> <many-to-one name="customer" class="Customer" column="lkm_cust_id"></many-to-one> </class>
Step 4: Establish the relationship between the two tables and the two entities in the mapping configuration
Characters use char or varchar in the database, but use String or Criteria in the entity class
- 1. Many-to-many relationship mapping configuration and its operation
- 1. Determine the relationship between the two tables
A user can have multiple roles
A role can be assigned to multiple roles
So it's many-to-many between used and role
- 2. The relationship between the two tables in the database is established
The realization of many-to-many in the database depends on the intermediate table
Only the primary keys of users and roles can appear in the intermediate table
- 3. Describe the relationship between two entity classes in the entity class
Each contains a collection reference to the other
Below is part of a mapping configuration file for a many-to-many relationship
<!-- set Used to map collection properties Attributes: name: used to specify the collection name table: refers to the name of the intermediate table (must be written in the middle of many-to-many tables) key: used to map foreign key fields The attribute column in key refers to the foreign key field name of the current entity in the intermediate table many-to-many: used to map many-to-many relationships Attributes: column: the foreign key field name of the other party in the intermediate table class: the entity class name of the other party --> <set name="roles" table="user_role_ref"> <key column="user_id"></key> <many-to-many class="SysRole" column="role_id"></many-to-many> </set>
The above is the way to create a joint primary key in the database
- 2. Perform many-to-many save operations
It is necessary to configure inverse=true in any set to let an entity class give up the maintenance relationship, so that the save function can be performed normally.
public class HibernateDemo1 { /** * save operation; * Create two users and three roles * Let user No. 1 have the roles of No. 1 and No. 2 * Let user No. 2 have the roles of No. 3 and No. 2 * Save users and roles */ @Test public void test1(){ SysUser u1=new SysUser(); u1.setUserName( "User1" ); SysUser u2=new SysUser(); u1.setUserName( "User2" ); SysRole r1=new SysRole(); r1.setRoleName( "Role 1" ); SysRole r2=new SysRole(); r1.setRoleName( "Role 2" ); SysRole r3=new SysRole(); r1.setRoleName( "Role 3" ); // Create a bidirectional relationship u1.getRoles (). add (r1); u1.getRoles (). add (r2); u2.getRoles (). add (r2); u2.getRoles (). add (r3); // create the character again r1.getUsers().add(u1); r2.getUsers().add(u1); r2.getUsers().add(u2); r3.getUsers().add(u2); Session s=HibernateUtils.getCurrentSession(); Transaction tx=s.beginTransaction(); s.save(r3); s.save(r2); s.save(r1); s.save(u1); s.save(u2); tx.commit(); }
- Delete operation (prohibited to use in actual development)
However, when cascading deletion is required, inverse=true of any configuration file of the cascade needs to be configured, and cascade=delete needs to be written in the configuration files of two many-to-many entity classes, otherwise the cascade deletion will not be performed.
<set name="users" table="user_role_ref" inverse="true" cascade="delete"> <key column="role_id"></key> <many-to-many class="SysUser" column="user_id"></many-to-many> </set>
/** * delete operation * In actual development, many-to-many bidirectional cascading deletion is prohibited */ @Test public void test2 () { Session s=HibernateUtils.getCurrentSession(); Transaction tx=s.beginTransaction(); // Query the user SysUser whose ID is 103 u =s.get(SysUser.class , 115L ); System.out.println(u); s.delete(u); tx.commit(); }