title: HIbernate slow data loading with collections
body: <p>I have a problem with a page loading of data. rich:datatable is used to display list of announcements.</p>
  
  <p>Announcement class include Set of AnnouncedLot class and in a mapping it is specified as</p>
  
  <pre><code>&lt;set cascade="all,delete-orphan" lazy="false" name="lots" table="ad_lot"&gt;
  &lt;key column="lotId"/&gt;
  &lt;many-to-many class="AnnouncedLot" column="anlotId"/&gt;
  &lt;/set&gt;
  </code></pre>
  
  <p>AnnouncedLot class mapping is </p>
  
  <pre><code>&lt;class name="AnnouncedLot" table="announcedLot"&gt;
    &lt;id name="id" type="java.lang.Long" unsaved-value="-1"&gt;
       &lt;generator class="native"/&gt;
    &lt;/id&gt;
    &lt;property name="idValue" not-null="false" unique="true"/&gt;
    &lt;property name="databaseId" not-null="false" unique="true"/&gt;
    &lt;property length="4047" name="title"/&gt;
    &lt;property length="4047" name="description"/&gt;
    &lt;property length="4047" name="source"/&gt;
    &lt;property length="65534" name="adText"/&gt;
    &lt;property name="sum"/&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>At the moment there are about 1,5 million of announcement in the database and 3,5 millions of lots. One announcement can have 1 or even 100 lots.</p>
  
  <p>Page loads only 30 first results of an announcement query. It loads quickly when there are no lots in the database, but with lots it can take around 1,5 min.</p>
  
  <p>I have tried to change lazy to true and to google this problem but haven't found much. Can anyone help with this?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>After checking what query takes a lot of time, I've found out that the rows counting query is the cause. Query contained lots.sum condition due to default filter parameter. I have change the filter so it doesn't have to use the set and the performance has improved.</p>
  
  <p>Thanks everyone for help.</p>