We are using the following HQL:
Code:
public Quote getSingleQuoteSummary(int quoteId) {
Query q = null;
Quote quote;
try {
q = getSession().createQuery(
" select q from Quote q" + " join fetch q.quoteStatus"
+ " left join fetch q.customer c"
+ " left join fetch q.licensee l"
+ " left join fetch q.payment"
+ " left join fetch c.shipDestination"
+ " left join fetch c.billTo"
+ " where q.id = :quote_id");
q.setInteger("quote_id", quoteId);
quote = (Quote) q.uniqueResult();
} catch (HibernateException ex) {
throw new DAOException(
ExceptionCodes.USERQUOTE_CANNOT_GET_USERQUOTE, ex
.getMessage());
}
return assignObservers(quote);
}
And here is the mapping metadata for the Quote object. As you can see it has a list association for line items which are lazy loaded.
Code:
<hibernate-mapping package="com.sybase.it.quoting.beans" default-cascade="none" default-lazy="true" auto-import="true">
<class name="com.sybase.it.quoting.beans.Quote" table="quote" dynamic-update="true" dynamic-insert="true" select-before-update="true" optimistic-lock="version">
<id name="quoteId" column="quote_id" type="java.lang.Integer">
<generator class="native"/>
</id>
<version name="version" column="version" type="java.lang.Short"/>
<property name="quoteName" column="quote_name" type="java.lang.String" not-null="true" />
<property name="licenseModel" column="license_model" type="java.lang.Short" />
<property name="supportProgram" column="support_program" type="java.lang.String" />
<property name="supportCatalog" column="support_catalog" type="java.lang.String" />
<property name="usageId" column="usage_id" type="java.lang.Integer" />
<property name="quoteDate" column="quote_date" type="java.util.Date" not-null="true" />
<property name="origAgreementDate" column="orig_agreement_date" type="java.util.Date" />
<property name="taxable" column="taxable" type="java.lang.Boolean" not-null="true" />
<property name="tin" column="tin" type="java.lang.String" />
<property name="shipCarrier" column="ship_carrier" type="java.lang.String" />
<property name="customerShipCarrier" column="customer_ship_carrier" type="java.lang.String" />
<property name="shipAccountNumber" column="ship_account_number" type="java.lang.String" />
<property name="publicVis" column="public_vis" type="java.lang.Boolean" not-null="true" />
<property name="orderNumber" column="order_number" type="java.lang.String" />
<property name="currencyCode" column="currency_code" type="java.lang.String" not-null="true" />
<property name="totalLicenseCost" column="total_license_cost" type="java.lang.Double" />
<property name="discountedLicenseCost" column="discounted_license_cost" type="java.lang.Double" />
<property name="totalSupportCost" column="total_support_cost" type="java.lang.Double" />
<property name="discountedSupportCost" column="discounted_support_cost" type="java.lang.Double" />
<property name="numLineItems" column="num_line_items" type="java.lang.Integer" />
<property name="numLicenses" column="num_licenses" type="java.lang.Integer" />
<property name="mgmtApproval" column="mgmt_approval" type="java.lang.Boolean" not-null="true" />
<property name="opsCenterCode" column="ops_center_code" type="java.lang.String" />
<property name="fobCode" column="fob_code" type="java.lang.String" />
<property name="dropShip" column="drop_ship" type="java.lang.Boolean" not-null="true" />
<property name="endUserKnown" column="end_user_known" type="java.lang.Boolean" not-null="true" />
<property name="comment" column="comment" type="java.lang.String" />
<property name="modBy" column="mod_by" type="java.lang.String" not-null="true" />
<property name="modDate" column="mod_date" type="java.util.Date" not-null="true" />
<property name="ownerLogin" column="owner_login" type="java.lang.String" not-null="true" />
<property name="lastOwnerLogin" column="last_owner_login" type="java.lang.String" />
<property name="expirationDate" column="expiration_date" type="java.util.Date" />
<property name="orderNotes" column="order_notes" type="java.lang.String" />
<property name="contractBeginDate" column="contract_begin_date" type="java.util.Date" />
<property name="contractEndDate" column="contract_end_date" type="java.util.Date" />
<property name="defaultSupportBeginDate" column="default_support_begin_date" type="java.util.Date" />
<property name="defaultSupportEndDate" column="default_support_end_date" type="java.util.Date" />
<many-to-one name="customer" column="customer_id" class="com.sybase.it.quoting.beans.Customer"/>
<many-to-one name="licensee" column="licensee" class="com.sybase.it.quoting.beans.Customer" outer-join="true"/>
<many-to-one name="quoteStatus" column="quote_status_id" class="com.sybase.it.quoting.beans.QuoteStatus" />
<many-to-one name="payment" column="payment_id" class="com.sybase.it.quoting.beans.Payment"/>
<list name="lineItems" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="quote_id"/>
<index column="list_index"/>
<one-to-many class="com.sybase.it.quoting.beans.QuoteLineitem"/>
</list>
<list name="salesReps" inverse="true" cascade="all" lazy="true">
<key column="quote_id"/>
<index column="list_index"/>
<one-to-many class="com.sybase.it.quoting.beans.SalesRep"/>
</list>
<list name="licenseDiscountThresholds" inverse="true" cascade="all" lazy="true">
<key column="quote_id"/>
<index column="list_index"/>
<one-to-many class="com.sybase.it.quoting.beans.LicenseDiscountThreshold"/>
</list>
<bag name="quoteMessages" inverse="true" cascade="all" lazy="true">
<key column="quote_id"/>
<one-to-many class="com.sybase.it.quoting.beans.QuoteMessage"/>
</bag>
<list name="jmsMessages" inverse="true" cascade="all" lazy="true">
<key column="quote_id"/>
<index column="list_index"/>
<one-to-many class="com.sybase.it.quoting.beans.JmsMessageTracker"/>
</list>
</class>
</hibernate-mapping>
We were thinking that by setting "fetch=join" in the lineItemDiscounts property of the QuoteLineItem mapping metadata (attached previously) that when the lineItems were retrieved with a getter that all associated line item discounts would be retreieved together in a single select statement.
Is it supposed to work this way OR is an explicit join fetch required in the HQL?
Code:
<bag name="lineItemDiscounts" inverse="true" cascade="all,delete-orphan" lazy="false" fetch="join">
<key column="lineitem_id"/>
<one-to-many class="com.sybase.it.quoting.beans.LineitemDiscount"/>
</bag>