-->
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.  [ 2 posts ] 
Author Message
 Post subject: SubqueryExpression throwing ClassCastException
PostPosted: Wed Sep 14, 2005 5:51 pm 
Newbie

Joined: Tue Nov 09, 2004 3:09 pm
Posts: 9
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 7:04 pm 
Newbie

Joined: Tue Nov 09, 2004 3:09 pm
Posts: 9
Note: I've posed a bug report and a proposed fix here:

http://opensource2.atlassian.com/projects/hibernate/browse/HHH-957


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