多對多相對于連接表的,有兩種
1、使用連接表的單向多對多關聯
?<class name="com.jason.associations.many2many.unilateralism.Person" table="Person">
??<id name="id" column="personId">
???<generator class="native"/>
??</id>
??<set name="addresses" table="PersonAddress" cascade="all">
???<key column="personId" not-null="true"/>
???<many-to-many
????column="addressId"
????class="com.jason.associations.many2many.unilateralism.Address"/>
??</set>
??<property name="name" column="personName" type="string" />
?</class>
?
?<class name="com.jason.associations.many2many.unilateralism.Address" table="Address">
??<id name="id" column="addressId">
???<generator class="native"/>
??</id>
??<property name="country" column="country" type="string" />
??<property name="city" column="city" type="string" />
?</class>
?<!--
??create table Person ( personId bigint not null primary key, personName varchar(20))
??create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId))
??create table Address ( addressId bigint not null primary key, country varchar(20), city varchar(20) )
?-->
?
2、使用連接表的雙向多對多關聯
?<class name="com.jason.associations.many2many.both.Person" table="Person">
??<id name="id" column="personId">
???<generator class="native"/>
??</id>
??<set name="addresses" table="PersonAddress" cascade="all">
???<key column="personId" not-null="true"/>
???<many-to-many
????column="addressId"
????class="com.jason.associations.many2many.both.Address"/>
??</set>
??<property name="name" column="personName" type="string" />
?</class>
?
?<class name="com.jason.associations.many2many.both.Address" table="Address">
??<id name="id" column="addressId">
???<generator class="native"/>
??</id>
??<set name="persons" table="PersonAddress" inverse="false" cascade="all">
???<key column="addressId" not-null="true"/>
???<many-to-many
????column="personId"
????class="com.jason.associations.many2many.both.Person"/>
??</set>
??<property name="country" column="country" type="string" />
??<property name="city" column="city" type="string" />
?</class>
?<!--
??create table Person ( personId bigint not null primary key, personName varchar(20))
??create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId))
??create table Address ( addressId bigint not null primary key, country varchar(20), city varchar(20) )
?-->