Hibernate multi-to-Multi-link ing (Table creation) and hibernate multi-to-many
Next we will describe Hibernate multi-to-Multi-link ing.
The structure of a Multi-to-Multi-link table is as follows:
The two entity tables also contain a relational table, which is a composite primary key. To use Hibernate multi-to-Multi-link ing, the relational table must contain only two fields, if multiple-to-multiple-link ing is generated for Hibernate, the intermediate relational table will not generate entities (that is, there is no corresponding pojo class, and there is no ing file ).
1. Create a table
- Drop table user_course;
- Drop table user;
- Drop table course;
- Create table user (
- Userid varchar (20) primary key,
- Name varchar (20) not null,
- Age int not null,
- Birthday date not null
- );
- Create table course (
- Id int primary key auto_increment,
- Title varchar (50) not null,
- Description text not null,
- Course_num int not null
- );
- Create table user_course (
- Userid varchar (20 ),
- Cid int,
- Primary key (userid, cid ),
- Foreign key (userid) references user (userid) on delete cascade,
- Foreign key (cid) references course (id) on delete cascade
- );
2. Generate Mappings
Select three tables to generate ing together. Note the following when selecting the primary key generation method:
Then, the primary key generation mode of each table is set independently, that is, click Next and set again. For the intermediate table, you do not need to select the primary key generation mode (see ).