|
It's a very simple condition.I build two class Book And BookDetail,In DB,Table BookDetail has a foreign key refers to the Table Book classes: public class Book { private int id; private String name; private BookDetail detail; }
public class BookDetail { private int id; private String summary; private Book book; }
hbm:
Book.hbm.xml: <class name="Book" table="Book" dynamic-insert="true" dynamic-update="true"> <id name="id" type="int"> <generator class="identity"></generator> </id>
<property name="name" type="string" not-null="true"/>
<one-to-one name="detail" class="BookDetail" property-ref="book"/> </class> BookDetail.hbm.xml: <class name="BookDetail" table="BookDetail" dynamic-insert="true" dynamic-update="true" > <id name="id" type="int"> <generator class="identity"></generator> </id> <property name="summary" type="string" />
<many-to-one name="book" column="book_id" class="Book" unique="true"/> </class>
The SqlQuery is simple: List<Book> booklist = session.createSQLQuery("select {b.*}
, {bd.*}
from Book as b left outer join BookDetail as bd on bd.book_id = b.id where b.id in (1,2,3)").addEntity("b",Book.class).addJoin("bd", "b", "detail").list();
When I execute the SqlQuery, The sql executed like n+1: Hibernate: select b.id as id1_0_0_, b.name as name2_0_0_,bd.id as id1_1_1_, bd.summary as summary2_1_1_, bd.book_id as book_id3_1_1_ from Book as b left outer join BookDetail as bd on bd.book_id = b.id where b.id in (1,2,3) Hibernate: select bookdetail0_.id as id1_1_0_, bookdetail0_.summary as summary2_1_0_, bookdetail0_.book_id as book_id3_1_0_ from BookDetail bookdetail0_ where bookdetail0_.book_id=? Hibernate: select bookdetail0_.id as id1_1_0_, bookdetail0_.summary as summary2_1_0_, bookdetail0_.book_id as book_id3_1_0_ from BookDetail bookdetail0_ where bookdetail0_.book_id=? Hibernate: select bookdetail0_.id as id1_1_0_, bookdetail0_.summary as summary2_1_0_, bookdetail0_.book_id as book_id3_1_0_ from BookDetail bookdetail0_ where bookdetail0_.book_id=?
The last 3 sql is unnecessary,addJoin is said to be fetch the BookDetail to the owner Book object.So I think it's a bug.AnyOne can Help me,thx
|