多对多相对于连接表的,有两种
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) )
-->