The env is simple,Two Classes Book and BookDetail,bi-directional one to one.The code is like below:
public class Book {
private int id;
private String name;
private BookDetail detail;
}
public class BookDetail {
private int id;
private String summary;
private Book book;
}
The hbm is simple:
<hibernate-mapping package="com.offia.domain">
<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>
</hibernate-mapping>
<hibernate-mapping package="com.offia.domain">
<class name="BookDetail" table="BookDetail" dynamic-insert="true" dynamic-update="true" batch-size="10">
<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>
</hibernate-mapping>
When I query the Book Object( not hope to fetch the BookDetail) like below:
List<Book> booklist = session.createQuery("from Book b").list();
Then the generated SQL is below
Hibernate: select book0_.id as id1_0_, book0_.name as name2_0_ from Book book0_
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=?
It like some N+1,Who can tell to how to avoid getting the BookDetail object(don't hope to get the last 3 sqls).
|