这是一个表结构:
<class name="test.Book" table="book" >
<cache usage="nonstrict-read-write"/>
<id column="id" name="id" type="int" unsaved-value="-1">
<generator class ="increment"/>
</id>
<property column="title" name="title" type="string" not-null="false" />
<property column="description" name="description" type="string" not-null="false" />
<list name="chapters" table="bookChapters" cascade="persist">
<cache usage="nonstrict-read-write"/>
<key column="bookChapter_id" />
<list-index column="rank"/>
<many-to-many column="chapter_id" class="test.Chapter" />
</list>
</class>
每次我拿到这本书时,它都会收集章节:
DetachedCriteria crit = DetachedCriteria.forClass(Book.class, id);
List<Book> bookList = getHibernateTemplate().findByCriteria(crit);
有时我需要一本没有章节集的书。如何用 Hibernate 做到这一点?