I'm doing a persistence framework evaluation and am currently working with Hibernate 2.1.2. One of the aspects I'm looking at is the degree to which we can eliminate embedded SQL from our application code, so I'd like to make use of the Criteria API as much as possible.
The following code works.
Code:
Query query = session.createSQLQuery(
"select distinct {ci.*} from carrier_invoice ci join cdr_" + aClient.getId()
+ " cd on ci.carrier_invoice_id=cd.carrier_invoice_id "
+ "where cd.service_id=:aService and ci.billing_period=:anInvoiceDate",
new String[]{
"ci"
},
new Class[]{
CarrierInvoice.class
}
);
query.setParameter("aService", aService.getId());
query.setParameter("anInvoiceDate", anInvoiceDate);
return (CarrierInvoice) query.uniqueResult();
In English, what I need to do is, given a "Client", "Service" and an invoice Date, find, in the Client's "CDR" table, the "CarrierInvoice" whose id appears in all the rows corresponding to the Service and invoice Date, and then create that CarrierInvoice from the CARRIER_INVOICE table.
(Yes, we have a few dynamic table names, but that's a whole other story...)
I'd like to do the same thing with the Criteria API but haven't figured out the right syntax or if it is even possible. Any suggestions?
Thanks,
Jim