天天看點

【hibernate架構】多對多雙向關聯(XML實作)

多對多的雙向關聯:(非常少用)

通過老師可以知道他教了多少學生,這是單項多對多。而如果同時通過學生知道有多少個老師教他,那麼就是多對多雙向的關聯。

xml實作:

student.java:

teacher.java:

配置檔案:

student.hbm.xml:

teacher.hbm.xml:

在hibernate.cfg.xml中配置:

生成的sql語句:

 create table t_s (

        student_id integer not null,

        teacher_id integer not null,

        primary key (teacher_id, student_id)

    )

    create table xm_student (

        id integer not null auto_increment,

        name varchar(255),

        primary key (id)

    create table xm_teacher (

    alter table t_s 

        add index fk1bf687b5893c2 (teacher_id), 

        add constraint fk1bf687b5893c2 

        foreign key (teacher_id) 

        references xm_teacher (id)

        add index fk1bf686abd4922 (student_id), 

        add constraint fk1bf686abd4922 

        foreign key (student_id) 

        references xm_student (id)

轉載請注明出處:http://blog.csdn.net/acmman