Hi all,
I am having a strange problem with a HQL query I am running.
My object model looks (simplified) like this:
Code:
CustomerQuoteRate:
Customer customer;
FreightClass freightClass;
{etc}
The query I am trying to run is as follows:
Code:
"SELECT DISTINCT cqr.freightClass "
+ "FROM " + CustomerQuotedRate.class.getName() + " AS cqr "
+ "WHERE (cqr.customer IS NULL OR cqr.customer.id = ?) "
+ "AND cqr.freightClass IS NOT NULL ";
This query is supposed to find and return a distinct list of freight classes, where such a freight class exists in the customer quoted rate table, for the customer in question (or the null customer).
Mr problem is, this query actually returns a list of CustomerQuotedRate instances, not a list of FreightClass instances. It is almost like the HQL parser is ignoring the SELECT portion of the query.
Any help would be appreciated.
regards,
Scott Russell
Hibernate version: 3.0.5
Mapping documents:Code:
<class name="CustomerQuotedRate" table="oxcqurte">
<!--cache usage="read-only"/-->
<cache usage="nonstrict-read-write"/>
<id name="docNo" column="doc_no" type="integer">
<generator class="identity"/>
</id>
<property name="fromDate" column="from_date" type="date"/>
<property name="expiryDate" column="exp_date" type="date"/>
<property name="minimumCharge" column="min_charge" type="double-null"/>
<property name="pickupCharge" column="pickup_charge" type="double-null"/>
<property name="deliveryCharge" column="del_charge" type="double-null"/>
<property name="glCode" column="gl_code" type="string-trimmed"/>
<property name="deptCode" column="dept_code" type="string-trimmed"/>
<many-to-one name="taxCode" column="tax_code" lazy="false"/>
<property name="updateID" column="update_id" type="string-trimmed"/>
<property name="updateDate" column="update_date" type="date"/>
<property name="wip" column="wip" type="wipType"/>
<property name="breakFlag" column="break_flag" type="breakFlagType"/>
<property name="contact" column="contact" type="string-trimmed"/>
<property name="phone" column="phone" type="string-trimmed"/>
<property name="status" column="status" type="string-trimmed"/>
<property name="negBy" column="neg_by" type="string-trimmed"/>
<property name="quoteDescription" column="quote_desc" type="string-trimmed"/>
<property name="subbyPercent" column="subby_percent" type="double-null"/>
<property name="paymentBase" column="pmt_base" type="string-trimmed"/>
<property name="nominalWeight" column="nominal_wt" type="double-null"/>
<property name="factor" column="factor" type="double-null"/>
<many-to-one name="freightClass" column="fr_class" lazy="false"/>
<many-to-one name="customer" column="cust_code" />
<many-to-one name="jobType" column="job_type" lazy="false"/>
<many-to-one name="serviceType" column="service_type" lazy="false"/>
<many-to-one name="vehicleClass" column="veh_class" lazy="false"/>
<many-to-one name="fromLocation" column="from_loc" lazy="false"/>
<many-to-one name="toLocation" column="to_loc" lazy="false"/>
<property name="fromType" column="from_type" type="string-trimmed"/>
<property name="toType" column="to_type" type="string-trimmed"/>
<list name="quantityBreaks" table="oxcqurtd" lazy="true" cascade="all">
<key column="doc_no"/>
<list-index column="line_no" base="1"/>
<composite-element class="CQRQuantityBreak">
<parent name="parent"/>
<property name="quantityBreak" column="qty_break" type="double-null"/>
<property name="rate" column="rate" type="double-null"/>
<property name="breakDescription" column="break_desc" type="string-trimmed"/>
<property name="breakNotes" column="break_notes" type="string-trimmed"/>
<property name="basicCharge" column="basic_charge" type="double-null"/>
</composite-element>
</list>
</class>
Code between sessionFactory.openSession() and session.close():Code:
public List<FreightClass> getFreightClassesForCustomer(String customerCode){
return getHibernateTemplate().find(findCustomerFreightClassesString, customerCode);
}
Full stack trace of any exception that occurs: N/A
Name and version of the database you are using: Informix 9.4
The generated SQL (show_sql=true):Code:
HQL: SELECT DISTINCT cqr.freightClass FROM com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate AS cqr WHERE (cqr.customer IS NULL OR cqr.customer.id = ?) AND cqr.freightClass IS NOT NULL
SQL: select distinct customerqu0_.doc_no as doc1_, customerqu0_.from_date as from2_11_, customerqu0_.exp_date as exp3_11_, customerqu0_.min_charge as min4_11_, customerqu0_.pickup_charge as pickup5_11_, customerqu0_.del_charge as del6_11_, customerqu0_.gl_code as gl7_11_, customerqu0_.dept_code as dept8_11_, customerqu0_.tax_code as tax9_11_, customerqu0_.update_id as update10_11_, customerqu0_.update_date as update11_11_, customerqu0_.wip as wip11_, customerqu0_.break_flag as break13_11_, customerqu0_.contact as contact11_, customerqu0_.phone as phone11_, customerqu0_.status as status11_, customerqu0_.neg_by as neg17_11_, customerqu0_.quote_desc as quote18_11_, customerqu0_.subby_percent as subby19_11_, customerqu0_.pmt_base as pmt20_11_, customerqu0_.nominal_wt as nominal21_11_, customerqu0_.factor as factor11_, customerqu0_.fr_class as fr23_11_, customerqu0_.cust_code as cust24_11_, customerqu0_.job_type as job25_11_, customerqu0_.service_type as service26_11_, customerqu0_.veh_class as veh27_11_, customerqu0_.from_loc as from28_11_, customerqu0_.to_loc as to29_11_, customerqu0_.from_type as from30_11_, customerqu0_.to_type as to31_11_ from oxcqurte customerqu0_ where (customerqu0_.cust_code is null or customerqu0_.cust_code=?) and (customerqu0_.fr_class is not null)
Debug level Hibernate log excerpt:Code:
2005-07-09 13:14:38,336 DEBUG [http-8080-Processor25] transaction.JDBCTransaction - begin
2005-07-09 13:14:38,336 DEBUG [http-8080-Processor25] transaction.JDBCTransaction - current autocommit status: true
2005-07-09 13:14:38,336 DEBUG [http-8080-Processor25] transaction.JDBCTransaction - disabling autocommit
2005-07-09 13:14:38,346 DEBUG [http-8080-Processor25] impl.SessionImpl - find: SELECT DISTINCT cqr.freightClass FROM com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate AS cqr WHERE (cqr.customer IS NULL OR cqr.customer.id = ?) AND cqr.freightClass IS NOT NULL
2005-07-09 13:14:38,406 DEBUG [http-8080-Processor25] engine.QueryParameters - parameters: [DUCATI]
2005-07-09 13:14:38,406 DEBUG [http-8080-Processor25] engine.QueryParameters - named parameters: {}
2005-07-09 13:14:38,416 DEBUG [http-8080-Processor25] ast.QueryTranslatorImpl - parse() - HQL: SELECT DISTINCT cqr.freightClass FROM com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate AS cqr WHERE (cqr.customer IS NULL OR cqr.customer.id = ?) AND cqr.freightClass IS NOT NULL
2005-07-09 13:14:38,446 DEBUG [http-8080-Processor25] ast.AST - --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| +-[FROM] 'FROM'
| | \-[RANGE] 'RANGE'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[IDENT] 'com'
| | | | | | | \-[IDENT] 'itglovebox'
| | | | | | \-[IDENT] 'smartmove'
| | | | | \-[IDENT] 'tpt'
| | | | \-[IDENT] 'rating'
| | | \-[IDENT] 'CustomerQuotedRate'
| | \-[ALIAS] 'cqr'
| \-[SELECT] 'SELECT'
| +-[DISTINCT] 'DISTINCT'
| \-[DOT] '.'
| +-[IDENT] 'cqr'
| \-[IDENT] 'freightClass'
\-[WHERE] 'WHERE'
\-[AND] 'AND'
+-[OR] 'OR'
| +-[IS_NULL] 'is null'
| | \-[DOT] '.'
| | +-[IDENT] 'cqr'
| | \-[IDENT] 'customer'
| \-[EQ] '='
| +-[DOT] '.'
| | +-[DOT] '.'
| | | +-[IDENT] 'cqr'
| | | \-[IDENT] 'customer'
| | \-[IDENT] 'id'
| \-[PARAM] '?'
\-[IS_NOT_NULL] 'is not null'
\-[DOT] '.'
+-[IDENT] 'cqr'
\-[IDENT] 'freightClass'
2005-07-09 13:14:38,446 DEBUG [http-8080-Processor25] ast.ErrorCounter - throwQueryException() : no errors
2005-07-09 13:14:38,446 DEBUG [http-8080-Processor25] antlr.HqlSqlBaseWalker - query() << begin, level = 1
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromElement - FromClause{level=1} : com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate (cqr) -> customerqu0_
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr -> customerqu0_.doc_no
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - getDataType() : freightClass -> org.hibernate.type.ManyToOneType(com.itglovebox.smartmove.tpt.ref.FreightClass)
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - dereferenceShortcut() : property freightClass in com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate does not require a join.
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr.freightClass -> customerqu0_.fr_class
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr -> customerqu0_.doc_no
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - getDataType() : customer -> org.hibernate.type.ManyToOneType(com.itglovebox.smartmove.ar.Customer)
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - dereferenceShortcut() : property customer in com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate does not require a join.
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr.customer -> customerqu0_.cust_code
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr -> customerqu0_.doc_no
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - getDataType() : customer -> org.hibernate.type.ManyToOneType(com.itglovebox.smartmove.ar.Customer)
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - dereferenceShortcut() : property id in com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate does not require a join.
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - Unresolved property path is now 'customer.id'
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr.customer -> customerqu0_.cust_code
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - getDataType() : customer.id -> org.hibernate.type.CustomType@13aac5f
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr.customer.id -> customerqu0_.cust_code
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr -> customerqu0_.doc_no
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - getDataType() : freightClass -> org.hibernate.type.ManyToOneType(com.itglovebox.smartmove.tpt.ref.FreightClass)
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.DotNode - dereferenceShortcut() : property freightClass in com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate does not require a join.
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.FromReferenceNode - Resolved : cqr.freightClass -> customerqu0_.fr_class
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] antlr.HqlSqlBaseWalker - query() : finishing up , level = 1
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.HqlSqlWalker - processQuery() : ( SELECT ( {select clause} DISTINCT ( customerqu0_.fr_class customerqu0_.doc_no freightClass ) ) ( FromClause{level=1} oxcqurte customerqu0_ ) ( WHERE ( AND ( OR ( is null ( customerqu0_.cust_code customerqu0_.doc_no customer ) ) ( = ( customerqu0_.cust_code ( customerqu0_.cust_code customerqu0_.doc_no customer ) id ) ? ) ) ( is not null ( customerqu0_.fr_class customerqu0_.doc_no freightClass ) ) ) ) )
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] ast.JoinProcessor - Using FROM fragment [oxcqurte customerqu0_]
2005-07-09 13:14:38,456 DEBUG [http-8080-Processor25] antlr.HqlSqlBaseWalker - query() >> end, level = 1
2005-07-09 13:14:38,466 DEBUG [http-8080-Processor25] ast.AST - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (oxcqurte)
+-[SELECT_CLAUSE] SelectClause: '{select clause}'
| +-[DISTINCT] SqlNode: 'DISTINCT'
| +-[DOT] DotNode: 'customerqu0_.doc_no as doc1_' {propertyName=freightClass,dereferenceType=ROOT_LEVEL,propertyPath=freightClass,path=cqr.freightClass,tableAlias=customerqu0_,className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate,classAlias=cqr}
| | +-[ALIAS_REF] IdentNode: 'customerqu0_.doc_no' {alias=cqr, className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate, tableAlias=customerqu0_}
| | \-[IDENT] IdentNode: 'freightClass' {originalText=freightClass}
| \-[SQL_TOKEN] SqlFragment: 'customerqu0_.from_date as from2_11_, customerqu0_.exp_date as exp3_11_, customerqu0_.min_charge as min4_11_, customerqu0_.pickup_charge as pickup5_11_, customerqu0_.del_charge as del6_11_, customerqu0_.gl_code as gl7_11_, customerqu0_.dept_code as dept8_11_, customerqu0_.tax_code as tax9_11_, customerqu0_.update_id as update10_11_, customerqu0_.update_date as update11_11_, customerqu0_.wip as wip11_, customerqu0_.break_flag as break13_11_, customerqu0_.contact as contact11_, customerqu0_.phone as phone11_, customerqu0_.status as status11_, customerqu0_.neg_by as neg17_11_, customerqu0_.quote_desc as quote18_11_, customerqu0_.subby_percent as subby19_11_, customerqu0_.pmt_base as pmt20_11_, customerqu0_.nominal_wt as nominal21_11_, customerqu0_.factor as factor11_, customerqu0_.fr_class as fr23_11_, customerqu0_.cust_code as cust24_11_, customerqu0_.job_type as job25_11_, customerqu0_.service_type as service26_11_, customerqu0_.veh_class as veh27_11_, customerqu0_.from_loc as from28_11_, customerqu0_.to_loc as to29_11_, customerqu0_.from_type as from30_11_, customerqu0_.to_type as to31_11_'
+-[FROM] FromClause: 'FROM' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[cqr], fromElementByTableAlias=[customerqu0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'oxcqurte customerqu0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=cqr,role=null,tableName=oxcqurte,tableAlias=customerqu0_,colums={,className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate}}
\-[WHERE] SqlNode: 'WHERE'
\-[AND] SqlNode: 'AND'
+-[OR] SqlNode: 'OR'
| +-[IS_NULL] SqlNode: 'is null'
| | \-[DOT] DotNode: 'customerqu0_.cust_code' {propertyName=customer,dereferenceType=ROOT_LEVEL,propertyPath=customer,path=cqr.customer,tableAlias=customerqu0_,className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate,classAlias=cqr}
| | +-[ALIAS_REF] IdentNode: 'customerqu0_.doc_no' {alias=cqr, className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate, tableAlias=customerqu0_}
| | \-[IDENT] IdentNode: 'customer' {originalText=customer}
| \-[EQ] SqlNode: '='
| +-[DOT] DotNode: 'customerqu0_.cust_code' {propertyName=id,dereferenceType=4,propertyPath=customer.id,path=cqr.customer.id,tableAlias=customerqu0_,className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate,classAlias=cqr}
| | +-[DOT] DotNode: 'customerqu0_.cust_code' {propertyName=id,dereferenceType=ROOT_LEVEL,propertyPath=customer.id,path=cqr.customer,tableAlias=customerqu0_,className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate,classAlias=cqr}
| | | +-[ALIAS_REF] IdentNode: 'customerqu0_.doc_no' {alias=cqr, className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate, tableAlias=customerqu0_}
| | | \-[IDENT] IdentNode: 'customer' {originalText=customer}
| | \-[IDENT] IdentNode: 'id' {originalText=id}
| \-[PARAM] SqlNode: '?'
\-[IS_NOT_NULL] SqlNode: 'is not null'
\-[DOT] DotNode: 'customerqu0_.fr_class' {propertyName=freightClass,dereferenceType=ROOT_LEVEL,propertyPath=freightClass,path=cqr.freightClass,tableAlias=customerqu0_,className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate,classAlias=cqr}
+-[ALIAS_REF] IdentNode: 'customerqu0_.doc_no' {alias=cqr, className=com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate, tableAlias=customerqu0_}
\-[IDENT] IdentNode: 'freightClass' {originalText=freightClass}
2005-07-09 13:14:38,466 DEBUG [http-8080-Processor25] ast.ErrorCounter - throwQueryException() : no errors
2005-07-09 13:14:38,466 DEBUG [http-8080-Processor25] ast.QueryTranslatorImpl - HQL: SELECT DISTINCT cqr.freightClass FROM com.itglovebox.smartmove.tpt.rating.CustomerQuotedRate AS cqr WHERE (cqr.customer IS NULL OR cqr.customer.id = ?) AND cqr.freightClass IS NOT NULL
2005-07-09 13:14:38,466 DEBUG [http-8080-Processor25] ast.QueryTranslatorImpl - SQL: select distinct customerqu0_.doc_no as doc1_, customerqu0_.from_date as from2_11_, customerqu0_.exp_date as exp3_11_, customerqu0_.min_charge as min4_11_, customerqu0_.pickup_charge as pickup5_11_, customerqu0_.del_charge as del6_11_, customerqu0_.gl_code as gl7_11_, customerqu0_.dept_code as dept8_11_, customerqu0_.tax_code as tax9_11_, customerqu0_.update_id as update10_11_, customerqu0_.update_date as update11_11_, customerqu0_.wip as wip11_, customerqu0_.break_flag as break13_11_, customerqu0_.contact as contact11_, customerqu0_.phone as phone11_, customerqu0_.status as status11_, customerqu0_.neg_by as neg17_11_, customerqu0_.quote_desc as quote18_11_, customerqu0_.subby_percent as subby19_11_, customerqu0_.pmt_base as pmt20_11_, customerqu0_.nominal_wt as nominal21_11_, customerqu0_.factor as factor11_, customerqu0_.fr_class as fr23_11_, customerqu0_.cust_code as cust24_11_, customerqu0_.job_type as job25_11_, customerqu0_.service_type as service26_11_, customerqu0_.veh_class as veh27_11_, customerqu0_.from_loc as from28_11_, customerqu0_.to_loc as to29_11_, customerqu0_.from_type as from30_11_, customerqu0_.to_type as to31_11_ from oxcqurte customerqu0_ where (customerqu0_.cust_code is null or customerqu0_.cust_code=?) and (customerqu0_.fr_class is not null)
2005-07-09 13:14:38,516 DEBUG [http-8080-Processor25] ast.ErrorCounter - throwQueryException() : no errors
2005-07-09 13:14:38,516 DEBUG [http-8080-Processor25] def.AbstractFlushingEventListener - flushing session
2005-07-09 13:14:38,516 DEBUG [http-8080-Processor25] def.AbstractFlushingEventListener - processing flush-time cascades