基于xml的多对多单向关联的关键配置:
many2many(xml):单向关联
|--->model
|--->Book
|--->private Set<Chapter> chapters = new HashSet<Chapter>();
|--->Chapter
|--->*.hbm.xml
|--->Book.hbm.xml
|---><set name="chapters" table="BOOK_CHAPTERS_1" cascade="save-update,remove">
<key name="book_id" />
<many-to-many class="Chapter" column="CHAPTER_ID"/>
</set>
|--->Chapter.hbm.xml
|--->hibernate.cfg.xml
|---><mapping resource="model/Book.hbm.xml" />
|---><mapping resource="model/Chapter.hbm.xml" />
基于annotation的多对多单向关联的关键配置:
many2many(anno):单向关联
|--->model
|--->Book
|--->private Set<Chapter> chapters = new HashSet<Chapter>();
@ManyToMany(targetEntity=Chapter.class)
@JoinTable(name="BOOK_CHAPTER_11",schema="hibernate",
joinColumns={@JoinColumn(name="BOOK_ID")},
inverseJoinColumns={@JoinColumn(name="CHAPTER_ID")}
)
@Cascade(value={CascadeType.SAVE_UPDATE,CascadeType.REMOVE})
|--->Chapter
|--->hibernate.cfg.xml
|---><mapping class="model.Book" />
|---><mapping class="model.Chapter" />
注意基于xml的和基于annotation的hibernate.xfg.xml配置文件中的<mapping>元素的不同