These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Help needed Hibernate fetching stategies
PostPosted: Sun Jan 10, 2010 10:59 am 
Newbie

Joined: Fri Jan 08, 2010 5:12 am
Posts: 4
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);
      }
   }


Top
 Profile  
 
 Post subject: Re: Help needed Hibernate fetching stategies
PostPosted: Tue Jan 12, 2010 2:45 pm 
Newbie

Joined: Wed Dec 23, 2009 12:38 pm
Posts: 14
Can you please enable stats and check the query execution count ...

SessionFactory sf = getSessions();

Statistics stats = sf.getStatistics();
boolean isStats = stats.isStatisticsEnabled();
stats.clear();
stats.setStatisticsEnabled(true);
session= sf.openSession();

....

System.out.println("## " + stats.getQueryExecutionCount());


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.