Thank you in advance to anyone who can offer any help with this. I am trying to use DetachedCriteria to add a subquery to a Criteria query in Hibernate 3.
The SubqueryExpression class throws a ClassCastException. Specifically, the toSqlString() method is attempting to cast a Criteria object to CriteriaImpl, but the underlying object is actually CriteriaImpl$Subcriteria.
My code is below, for what it's worth. Obviously there's only so much you can tell without seeing all of my data layer classes and hbm files. Has anyone encountered this problem before? Is there some obvious or not-so-obvious mistake I've made?
Thanks.
--Rob
Hibernate version: 3
Code between sessionFactory.openSession() and session.close():
Criteria c = s.createCriteria(Invoice.class);
//Status
if (params.getStatusTypeID() != null) {
//"stat" below is an alias.
Criteria cStatus = c.createCriteria("InvoiceStatusHistory", "stat");
Criteria cType = cStatus.createCriteria(InvoiceStatus.PROP_STATUS_TYPE);
cType.add(Restrictions.eq(Type.PROP_ID, params.getStatusTypeID()));
//Subquery
DetachedCriteria currStatNum = DetachedCriteria.forClass(InvoiceStatus.class, "currStat");
currStatNum.setProjection( Property.forName(InvoiceStatus.PROP_STATUS_NUMBER).max() );
currStatNum.add(Property.forName("currStat.Invoice").eqProperty("stat.Invoice"));
cStatus.add(Property.forName(InvoiceStatus.PROP_STATUS_NUMBER).eq(currStatNum));
}
//invoice number
if (params.getVendorInvoiceId() != null) {
c.add(Restrictions.eq(Invoice.PROP_VENDOR_INVOICE_ID, params.getVendorInvoiceId()));
}
//date range start date.
if (params.getRangeStartDate() != null) {
c.add(Restrictions.ge(Invoice.PROP_INVOICE_DATE, params.getRangeStartDate()));
}
//date range end date.
if (params.getRangeEndDate() != null) {
c.add(Restrictions.le(Invoice.PROP_INVOICE_DATE, params.getRangeEndDate()));
}
//Vendor id
if (params.getVendorId() != null) {
c.add(Restrictions.eq(Invoice.PROP_VENDOR_ID, params.getVendorId()));
}
//Order number
if (params.getOrderId() != null) {
c.add(Restrictions.eq(Invoice.PROP_ORDER_ID, params.getOrderId()));
}
List rawList = c.list();
Iterator itr = rawList.iterator();
while(itr.hasNext()) {
Invoice invoice = (Invoice)itr.next();
System.out.println(invoice.getVendorInvoiceId());
}
Full stack trace of any exception that occurs:
java.lang.ClassCastException
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:43)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:314)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1303)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at com.netjets.crossroads.data.TestQueries.testSearchInvoice(TestQueries.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
Name and version of the database you are using: oracle 9i
|