-->
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.  [ 7 posts ] 
Author Message
 Post subject: Named parameter does not exist...
PostPosted: Tue Feb 10, 2004 12:27 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
QUERY MAPPING
Code:
<hibernate-mapping>
    <query name="test.by.test.containing.string">
        <![CDATA[
            from com.<package-name>.test.Test as test
            where (test.test LIKE '%:searchString%')
        ]]>
    </query>
</hibernate-mapping>


<package-name> is not the actual value in the package String, just preserving some anonymity :-)

Anyways, executing the following code throws the exception listed at the bottom:

CODE
Code:
// QUERY NAMES
private static final String LIST_TEST__BY_TEST_CONTAINING_STRING = "test.by.test.containing.string";

// QUERY PARAMS
private static final String SEARCH_STRING = "searchString";

public List queryByTestContainingString(String searchString) {
    Session session = null;
    Query query = null;

    try {
        // open session
        session = sessionFactory.openSession();

        // create query
        query = session.createQuery(LIST_TEST__BY_TEST_CONTAINING_STRING);

        // bind parameters to query
        query.setString(SEARCH_STRING, searchString);

        // perform query
        List results = query.list();

        // close session
        closeSession(session);

        // return results
        return results;
    } catch (HibernateException e) {
        log.fatal("Error performing query: " + query.getQueryString(), e);
        closeSession(session);
        return null;
    }
}


EXCEPTION
Code:
java.lang.IllegalArgumentException: Parameter searchString does not exist as a named parameter in [test.by.test.containing.string]
   net.sf.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:326)
   net.sf.hibernate.impl.AbstractQueryImpl.setString(AbstractQueryImpl.java:345)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 12:47 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
stepping through the hibernate code, i noticed something. The queryString that is being passed into the StringTokenizer is actually the query name from the mapping document above instead of the query itself, or, in other words, it looks like it's parsing the following string for query parameters:

"test.by.test.containing.string"

Maybe the syntax of my mapping is incorrect???


AbstractQueryImpl: initParameterBookKeeping()
lines 455 - 467
Code:
private void initParameterBookKeeping() {
    StringTokenizer st = new StringTokenizer(queryString, ParserHelper.HQL_SEPARATORS);
    Set result = new HashSet();

    while ( st.hasMoreTokens() ) {
        String string = st.nextToken();
        if( string.startsWith(ParserHelper.HQL_VARIABLE_PREFIX) ) {
            result.add( string.substring(1) );
        }
    }
   
    actualNamedParameters = result;
    positionalParameterCount = StringHelper.countUnquoted(queryString, '?');
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 12:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You must add the % in the java code to the string, can't do in the query.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 1:11 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
I changed the search string as per your suggestion, but I'm still getting the same error. Basically what's happening is that when it goes to parse the queryString in the initParameterBookKeeping() method, the queryString is, for some reason, set to the query name:

test.by.test.containing.string

and it is parsing it for named parameters...which is obviously not what we want :-)

So I guess the next place to look is where the queryString property is getting set...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 1:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Ah yes ... use session.getNamedQuery() as clearly stated in the docs http://www.hibernate.org/hib_docs/reference/html/manipulating-data.html#manipulating-data-s5


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 1:16 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
lol, i just figured that out...

doh!

thx :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 2:08 pm 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
SOLUTION


QUERY MAPPING
Code:
<query name="test.by.test.containing.string"> <![CDATA[
    from Test as test
        where (test.test LIKE :searchString)
]]></query>



QUERY STRING
Code:
// query by search string
List queryResults = testDAO.queryByTestContainingString("%<br>%");



One thing to note for this example is to not use single quotes in the query string as is standard with normal SQL syntax:

e.g. SELECT .... WHERE (<param> LIKE '<query-string>')

When I included the single quotes (e.g. [i]queryByTestContainingString("'%<br>%'"), the query returned nothing.


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