I am working on hibernate fetching strategies but unable to get the difference. I do not know where I am doing a mistake. I am using MySQL5.0 as DB.
I have an Author class and a Books class. The relationship is One to many. I query the books and retrieve the author one by one but I am not able to see the select query for each author for each book from the list obtained after executing the query. This is the default fetching strategy but I am not able to see the query but able to print the properties of the author.
I have added the following two properties in the hibernate.cfg.xml. Are there any other property which needs to be added for sql logging apart from the below.
Code:
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
I am hereby attaching the code for more details.
Books.hbm file
Code:
<hibernate-mapping package="com.local.hibernate.author">
<class name="Books" table="BOOKS">
<id column="BOOK_ID" name="sbnId">
<generator class="increment"></generator>
</id>
<property name="edition" column="bOOKS_EDITION"></property>
<property name="revision" column="BOOKS_REVISION"></property>
<many-to-one name="author" class="Author" column="AUTHOR_BOOK_ID"
not-null="true">
</many-to-one>
</class>
</hibernate-mapping>
Author .hbm file
Code:
<hibernate-mapping package="com.local.hibernate.author">
<class name="Author" table="AUTHOR">
<id name="authorId" column="AUTHOR_ID">
<generator class="increment"></generator>
</id>
<property name="name" column="AUTHOR_NAME"></property>
<property name="dob" column="AUTHOR_DOB"></property>
<property name="city" column="AUTHOR_CITY"></property>
<property name="state" column="AUTHOR_STATE"></property>
<set name="books"
cascade="save-update,delete,delete-orphan"
inverse="true" >
<key column="AUTHOR_BOOK_ID"></key>
<one-to-many class="Books" />
</set>
</class>
</hibernate-mapping>
In the client code
Code:
List<Books> results = session.createQuery("from Books book").list();
processBooks(results);
}
private static void processBooks(List<Books> list) {
Iterator<Books> iter = list.iterator();
if (!iter.hasNext()) {
System.out.println("No Books to display.");
return;
}
while (iter.hasNext()) {
Books book = (Books) iter.next();
String msg = book.getAuthor().getName() + "\t";
//String msg = "\t";
msg += book.getAuthor() + "\t";
msg += book.getEdition() + "\t";
msg += book.getRevision();
System.out.println(msg);
}
}