Hibernate multi-to-Multi-link ing (Table creation) and hibernate multi-to-many

Source: Internet
Author: User

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

  1. Drop table user_course;
  2. Drop table user;
  3. Drop table course;
  4. Create table user (
  5. Userid varchar (20) primary key,
  6. Name varchar (20) not null,
  7. Age int not null,
  8. Birthday date not null
  9. );
  10. Create table course (
  11. Id int primary key auto_increment,
  12. Title varchar (50) not null,
  13. Description text not null,
  14. Course_num int not null
  15. );
  16. Create table user_course (
  17. Userid varchar (20 ),
  18. Cid int,
  19. Primary key (userid, cid ),
  20. Foreign key (userid) references user (userid) on delete cascade,
  21. Foreign key (cid) references course (id) on delete cascade
  22. );

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 ).

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.