gloeglm wrote:
I am not so sure this is legal ... should not be used in the where clause.
Didn't think so. I was sort of posting just to confirm this.
Quote:
Wouldn't a normal left join initialize the collection, too?
Doesn't seem to. With the mappings listed above, the following HQL results in 1+N queries :-(
Code:
Object[] values = new Object[] { vendorId, status.getId() };
Type[] types= new Type[] { Hibernate.STRING, Hibernate.INTEGER };
String query =
"select purchaseOrder from PurchaseOrderMetaData purchaseOrder " +
"left join purchaseOrder.actions action " +
"where purchaseOrder.vendor.vendorId = ? " +
"and action.status = ? ";
List orders = HibernateUtils.find(query, values, types);
Here is the log (evidence of the 1+N):
Code:
[DEBUG] 13:51:06 BatcherImpl - prepared statement get: select purchase0_.purchase_order_id as purchase_order_id, purchase0_.vendor_id as vendor_id, purchase0_.store_no as store_no, purchase0_.purchase_order_no as purchase4_, purchase0_.created_dtm as created_5_, purchase0_.mailbag_id as mailbag_id, purchase0_.store_type as store_type, purchase0_.po_type as po_type, purchase0_.po_file_date as po_file_9_, purchase0_.do_not_ship_before_date as do_not_10_, purchase0_.cancel_if_not_shipped_date as cancel_11_ from purchase_order purchase0_, purchase_order_action actions1_ where purchase0_.purchase_order_id=actions1_.purchase_order_id(+) and ((purchase0_.vendor_id=? )and(actions1_.po_status_id=? ))
[DEBUG] 13:51:06 BatcherImpl - preparing statement
[DEBUG] 13:51:06 StringType - binding '20138' to parameter: 1
[DEBUG] 13:51:06 IntegerType - binding '1' to parameter: 2
[DEBUG] 13:51:06 Loader - processing result set
[DEBUG] 13:51:06 IntegerType - returning '3' as column: purchase_order_id
[DEBUG] 13:51:06 Loader - result row: 3
[DEBUG] 13:51:06 Loader - Initializing object from ResultSet: 3
[DEBUG] 13:51:06 Loader - Hydrating entity: com.michaels.webedi.mailbox.data.PurchaseOrderMetaData#3
[DEBUG] 13:51:06 StringType - returning '20138' as column: vendor_id
[DEBUG] 13:51:06 StringType - returning '2723' as column: store_no
[DEBUG] 13:51:06 StringType - returning '900194' as column: purchase4_
[DEBUG] 13:51:06 StringType - returning '20031218' as column: created_5_
[DEBUG] 13:51:06 StringType - returning '000BBA' as column: mailbag_id
[DEBUG] 13:51:06 StringType - returning 'STOR' as column: store_type
[DEBUG] 13:51:06 StringType - returning 'POR' as column: po_type
[DEBUG] 13:51:06 StringType - returning '20031218' as column: po_file_9_
[DEBUG] 13:51:06 StringType - returning '20031218' as column: do_not_10_
[DEBUG] 13:51:06 StringType - returning '20040101' as column: cancel_11_
This occurs 80 more times, once for each purchase order
Code:
[DEBUG] 13:51:13 Loader - done processing result set (81 rows)
[DEBUG] 13:51:13 BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
[DEBUG] 13:51:13 BatcherImpl - closing statement
[DEBUG] 13:51:13 Loader - total objects hydrated: 81
[DEBUG] 13:51:13 SessionImpl - resolving associations for [com.michaels.webedi.mailbox.data.PurchaseOrderMetaData#3]
[DEBUG] 13:51:13 SessionImpl - loading [com.michaels.webedi.mailbox.service.vendor.Vendor#20138]
[DEBUG] 13:51:13 SessionImpl - attempting to resolve [com.michaels.webedi.mailbox.service.vendor.Vendor#20138]
[DEBUG] 13:51:13 SessionImpl - resolved object in session cache [com.michaels.webedi.mailbox.service.vendor.Vendor#20138]
[DEBUG] 13:51:13 SessionImpl - collection not cached
[DEBUG] 13:51:13 SessionImpl - done materializing entity [com.michaels.webedi.mailbox.data.PurchaseOrderMetaData#3]
This happens 80 more times.
Code:
[DEBUG] 13:51:15 SessionImpl - initializing non-lazy collections
[DEBUG] 13:51:15 SessionImpl - initializing collection [com.michaels.webedi.mailbox.data.PurchaseOrderMetaData.actions#174]
[DEBUG] 13:51:15 BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets
[DEBUG] 13:51:15 BatcherImpl - prepared statement get: select purchase0_.user_no as user_no__, purchase0_.action_dtm as action_dtm__, purchase0_.po_status_id as po_statu4___, purchase0_.verified_ind as verified5___, purchase0_.purchase_order_id as purchase1___ from purchase_order_action purchase0_ where purchase0_.purchase_order_id=?
[DEBUG] 13:51:15 BatcherImpl - preparing statement
[DEBUG] 13:51:15 IntegerType - binding '174' to parameter: 1
[DEBUG] 13:51:15 Loader - result set contains (possibly empty) collection: [com.michaels.webedi.mailbox.data.PurchaseOrderMetaData.actions#174]
[DEBUG] 13:51:15 SessionImpl - uninitialized collection: initializing
[DEBUG] 13:51:15 Loader - processing result set
[DEBUG] 13:51:15 Loader - result row:
[DEBUG] 13:51:15 IntegerType - returning '174' as column: purchase1___
[DEBUG] 13:51:15 Loader - found row of collection: [com.michaels.webedi.mailbox.data.PurchaseOrderMetaData.actions#174]
[DEBUG] 13:51:15 SessionImpl - reading row
[DEBUG] 13:51:15 IntegerType - returning null as column: user_no__
[DEBUG] 13:51:15 TimestampType - returning '22 December 2003 08:57:15' as column: action_dtm__
[DEBUG] 13:51:15 IntegerType - returning '1' as column: po_statu4___
[DEBUG] 13:51:15 IntegerType - returning '1' as column: verified5___
[DEBUG] 13:51:15 Loader - done processing result set (1 rows)
[DEBUG] 13:51:15 BatcherImpl - done closing: 0 open PreparedStatements, 0 open ResultSets
[DEBUG] 13:51:15 BatcherImpl - closing statement
[DEBUG] 13:51:15 SessionImpl - collection fully initialized: [com.michaels.webedi.mailbox.data.PurchaseOrderMetaData.actions#174]
[DEBUG] 13:51:15 SessionImpl - 1 collections initialized
This happens 80 times. Yuck. So...is it possible to find a list of objects using criteria found in child records *and* eagerly load these children with one query?
Ryan