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.  [ 3 posts ] 
Author Message
 Post subject: Paging a Class containing eagerly fetched collections
PostPosted: Mon Feb 13, 2006 9:06 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Hibernate version:
3.1.2

Mapping documents:
EmailAddress
Code:
  <class name="org.tecas.model.EmailAddress" table="EMAIL_ADDRESS">
    <id name="id" column="ID" type="long" unsaved-value="null" >
      <generator class="native"/>
    </id>

    <property name="address" column="ADDRESS" unique="true"/>
    <property name="addedDate" column="ADDED_DATE"/>
    <property name="confirmed" column="CONFIRMED" type="boolean"/>

    <property name="dailySummary" column="DAILY_SUMMARY"/>
    <property name="delimiter" column="DELIMITER"/>
    <property name="filter" column="FILTER"/>

    <list name="emailCodes" cascade="all,delete-orphan" lazy="false" fetch="join">
      <key column="EMAIL_ADDRESS_ID"/>
      <index column="POS"/>
      <one-to-many class="org.tecas.model.EmailCode"/>
    </list>

    <property name="monthlySummary" column="MONTHLY_SUMMARY"/>
    <property name="nextDailySummaryDate" column="NEXT_DAILY_SUMMARY_DATE"/>

    <property name="statBlacklistEmails" column="STAT_BLACKLIST_EMAILS"/>
    <property name="statConfirmedEmails" column="STAT_CONFIRMED_EMAILS"/>
    <property name="statDeletedEmails" column="STAT_DELETED_EMAILS"/>
    <property name="statManuallyConfirmedEmails" column="STAT_MANUALLY_CONFIRMED_EMAILS"/>
    <property name="statManuallyDeletedEmails" column="STAT_MANUALLY_DELETED_EMAILS"/>
    <property name="statStoredEmails" column="STAT_STORED_EMAILS"/>
    <property name="statValidCodeEmails" column="STAT_VALID_CODE_EMAILS"/>
    <property name="statWhitelistEmails" column="STAT_WHITELIST_EMAILS"/>

    <set name="storedEmail" table="STORED_EMAIL" cascade="all,delete-orphan" lazy="true" fetch="join" inverse="true">
      <key column="EMAIL_ADDRESS_ID"/>
      <one-to-many class="org.tecas.model.StoredEmail"/>
    </set>

    <property name="storedEmailCount" formula="(select count(*) from STORED_EMAIL storedEmail WHERE storedEmail.EMAIL_ADDRESS_ID = id and storedEmail.deliver = 0)"/>


    <many-to-one name="user" column="USER_ID"/>
    <property name="weeklySummary" column="WEEKLY_SUMMARY"/>

  </class>


Java Types for EmailAddress
Code:
  private Long id;
  private String address;
  private Date addedDate;
  private Boolean confirmed;
  private User user;
  private List<EmailCode> emailCodes;
  private Set<StoredEmail> storedEmail;
  private Long storedEmailCount;
  private String delimiter;
  private boolean filter = true;
  private boolean dailySummary = false;
  private boolean monthlySummary = false;
  private boolean weeklySummary = false;
  private Date nextDailySummaryDate;

  // stats
  private Long statBlacklistEmails;
  private Long statConfirmedEmails;
  private Long statDeletedEmails;
  private Long statManuallyConfirmedEmails;
  private Long statManuallyDeletedEmails;
  private Long statStoredEmails;
  private Long statValidCodeEmails;
  private Long statWhitelistEmails;

StoredEmail
Code:
  <class name="org.tecas.model.StoredEmail" table="STORED_EMAIL">
    <id name="id" column="ID" type="long" unsaved-value="null">
      <generator class="native"/>
    </id>

    <property name="bodyText" column="BODY_TEXT"/>
    <property name="deliver" column="DELIVER"/>
    <many-to-one name="emailAddress" column="EMAIL_ADDRESS_ID" not-null="true"/>
    <property name="email" column="EMAIL"/>
    <property name="senderAddress" column="SENDER_ADDRESS"/>
    <property name="size" column="SIZE"/>
    <property name="storedDate" column="STORED_DATE"/>
    <property name="subject" column="SUBJECT"/>
  </class>

Java properties for StoredEmail
Code:
  private Long id;
  private EmailAddress emailAddress;
  private Date storedDate;
  private String bodyText;
  private String email;
  private String senderAddress;
  private String subject;
  private Integer size;
  private boolean deliver = false;

Code between sessionFactory.openSession() and session.close():
Code:
        Criteria crit = session.createCriteria(EmailAddress.class);
        crit.add(Expression.eq("weeklySummary", true));

        crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        crit.setFirstResult(firstResult);
        crit.setMaxResults(maxResults);

        crit.addOrder(Order.asc("id"));

        return crit.list();

I have also tried the following HQL:
"from org.tecas.model.EmailAddress emailAddress
left join fetch emailAddress.storedEmail left join fetch emailAddress.emailCodes
where emailAddress.weeklySummary = :weeklySummary order by emailAddress.id"

Name and version of the database you are using:
MySQL 4.1.8

The generated SQL (show_sql=true):
From the Criteria:
Hibernate: select top ? this_.ID as ID0_2_, this_.ADDRESS as ADDRESS0_2_, this_.ADDED_DATE as ADDED3_0_2_, this_.CONFIRMED as CONFIRMED0_2_, this_.DAILY_SUMMARY as DAILY5_0_2_, this_.DELIMITER as DELIMITER0_2_, this_.FILTER as FILTER0_2_, this_.MONTHLY_SUMMARY as MONTHLY8_0_2_, this_.NEXT_DAILY_SUMMARY_DATE as NEXT9_0_2_, this_.STAT_BLACKLIST_EMAILS as STAT10_0_2_, this_.STAT_CONFIRMED_EMAILS as STAT11_0_2_, this_.STAT_DELETED_EMAILS as STAT12_0_2_, this_.STAT_MANUALLY_CONFIRMED_EMAILS as STAT13_0_2_, this_.STAT_MANUALLY_DELETED_EMAILS as STAT14_0_2_, this_.STAT_STORED_EMAILS as STAT15_0_2_, this_.STAT_VALID_CODE_EMAILS as STAT16_0_2_, this_.STAT_WHITELIST_EMAILS as STAT17_0_2_, this_.USER_ID as USER18_0_2_, this_.WEEKLY_SUMMARY as WEEKLY19_0_2_, (select count(*) from STORED_EMAIL storedEmail WHERE storedEmail.EMAIL_ADDRESS_ID = this_.id and storedEmail.deliver = 0) as formula0_2_, emailcodes2_.EMAIL_ADDRESS_ID as EMAIL4_4_, emailcodes2_.ID as ID4_, emailcodes2_.POS as POS4_, emailcodes2_.ID as ID1_0_, emailcodes2_.CODE as CODE1_0_, emailcodes2_.NAME as NAME1_0_, storedemai3_.EMAIL_ADDRESS_ID as EMAIL4_5_, storedemai3_.ID as ID5_, storedemai3_.ID as ID4_1_, storedemai3_.BODY_TEXT as BODY2_4_1_, storedemai3_.DELIVER as DELIVER4_1_, storedemai3_.EMAIL_ADDRESS_ID as EMAIL4_4_1_, storedemai3_.EMAIL as EMAIL4_1_, storedemai3_.SENDER_ADDRESS as SENDER6_4_1_, storedemai3_.SIZE as SIZE4_1_, storedemai3_.STORED_DATE as STORED8_4_1_, storedemai3_.SUBJECT as SUBJECT4_1_ from EMAIL_ADDRESS this_ left outer join EMAIL_CODE emailcodes2_ on this_.ID=emailcodes2_.EMAIL_ADDRESS_ID left outer join STORED_EMAIL storedemai3_ on this_.ID=storedemai3_.EMAIL_ADDRESS_ID where this_.WEEKLY_SUMMARY=? order by this_.ID asc

Question:
So my situation is I may have thousands of EmailAddresses that will be returned from this query so I need to page it and deal with maybe 100 at a time. I also need the class to be populated with at a minimum the storedEmail set, because I can't be executing a load query for the storedEmail set on every one of the EmailAddresses. However I have tried working with .setFirstResult and when I set it to 1 it still returns only the first result rather than the second, and when I try setting firstResult to 0 and maxResults to 5 it only returns one result when there should be 5. I realize this may be a problem with having the joins in the same select, can anyone suggest a way for me to do this?

Question 2:
Assuming someone can help me solve the first question, would it be possible to further restrict the storedEmail set that is fetched for the parent EmailAddress class based on the storedEmail.storedDate property? IE stored within a certain date range?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 5:46 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
bump, any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 15, 2006 8:41 am 
Newbie

Joined: Thu Oct 09, 2003 6:57 am
Posts: 17
Location: France
Hello,

I hope that the "distinct" functionnality will appear in hibernate as asked here : http://opensource2.atlassian.com/projects/hibernate/browse/HB-520

For me,the collections of my parent class are feching lazily. So, I can do pagination without problems.

I don't find a way to resolve your problem with eagily fetched and criteria. So if you find one please tell me.

And sorry for my english language


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.