I have the following class defined in my mapping XML file:
<class name='com.data.StateRefData' table='STATE_REF'> <composite-id name='primaryKey' class='com.data.StateRefPkData'> <key-property name='countryCode'> <column name='COUNTRY_CODE' /> </key-property> <key-property name='state'> <column name='STATE' /> </key-property> </composite-id> <property name='dialingCode' column='DIALING_CODE'></property> <property name='isActive' column='IS_ACTIVE'></property> <property name='localeCode' column='LOCALE_CODE'></property> <property name='stName' column='ST_NAME'></property> </class>
I am trying to define a NameQuery for this class. What I tried was:
<query name='findState'> <![CDATA[ from com.data.StateRefData WHERE primaryKey = :primaryKey ]]> </query>
The Java code that I use to call this is:
Query stateQuery = null; List<StateRefData> stateList = null; StateRefPkData primaryKey = new StateRefPkData(); StateRefData filter = new StateRefData(); primaryKey.setCountryCode(inCountryCode); primaryKey.setState(inStateProvince); filter.setPrimaryKey(primaryKey); stateQuery = session.getNamedQuery('findState'); stateQuery.setProperties(filter);
However, I get these errors right at the ‘stateQuery.list()’ part:
[ERROR JDBCExceptionReporter:72] Line 1: Incorrect syntax near ','. org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.loader.Loader.doList(Loader.java:1596) at org.hibernate.loader.Loader.list(Loader.java:1577) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395) at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
Any idea on how to use a NamedQuery with a composite?
Note: I have also tried doing:
stateQuery.setParameter('primaryKey', primaryKey);
and it still has the error.
New (edited): Here is the HQL and SQL that happens when I execute the code:
[DEBUG AST:223] --- HQL AST --- \-[QUERY] 'query' +-[SELECT_FROM] 'SELECT_FROM' | \-[FROM] 'from' | \-[RANGE] 'RANGE' | +-[DOT] '.' | | +-[DOT] '.' | | | +-[DOT] '.' | | | | +-[DOT] '.' | | | | | +-[IDENT] 'com' | | | \-[IDENT] 'data' | | \-[IDENT] 'StateRefData' | \-[ALIAS] 'd' \-[WHERE] 'WHERE' \-[EQ] '=' +-[DOT] '.' | +-[IDENT] 'd' | \-[IDENT] 'primaryKey' \-[COLON] ':' \-[IDENT] 'primaryKey' [DEBUG AST:193] --- SQL AST --- \-[SELECT] QueryNode: 'SELECT' querySpaces (STATE_REF) +-[SELECT_CLAUSE] SelectClause: '{derived select clause}' | +-[SELECT_EXPR] SelectExpressionImpl: 'statere0_.COUNTRY_CODE as COUNTRY1_, statere0_.STATE as STATE' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=d,role=null,tableName=STATE_REF,tableAlias=statere0_,colums={,className=com.data.StateRefData}}} | \-[SQL_TOKEN] SqlFragment: 'statere0_.DIALING_CODE as DIALING3_60_, statere0_.IS_ACTIVE as IS4_60_, statere0_.LOCALE_CODE as LOCALE5_60_, statere0_.ST_NAME as ST6_60_' +-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[d], fromElementByTableAlias=[statere0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]} | \-[FROM_FRAGMENT] FromElement: 'STATE_REF statere0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=d,role=null,tableName=STATE_REF,tableAlias=statere0_,colums={,className=com.data.StateRefData}} \-[WHERE] SqlNode: 'WHERE' \-[EQ] SqlNode: '=' +-[DOT] DotNode: '(statere0_.COUNTRY_CODE, statere0_.STATE)' {propertyName=primaryKey,dereferenceType=2,propertyPath=primaryKey,path=d.primaryKey,tableAlias=statere0_,className=com.data.StateRefData,classAlias=d} | +-[ALIAS_REF] IdentNode: '(statere0_.COUNTRY_CODE, statere0_.STATE)' {alias=d, className=com.data.StateRefData, tableAlias=statere0_} | \-[IDENT] IdentNode: 'primaryKey' {originalText=primaryKey} \-[NAMED_PARAM] SqlNode: '?'
Can you provide the SQL code that is being generated by turning on SQL logging?
Here is a sample if you were using log4j.properties:
log4j.logger.org.hibernate.SQL=DEBUG
UPDATE #1
I am not sure why the existing query does not work, but you should also give this a try. No need to update any other code as passing a POJO should automatically bind the getters to the named parameters.