query syntax problem

Posted on 2011-04-20
Medium Priority
Last Modified: 2012-05-11
I just don't know what is wrong with this statement?

 public static TypedQuery<KeyWord> findKeyWordsByKeywordEquals(String keyword) {
    	String webprofile = "44";
        if (keyword == null || keyword.length() == 0) throw new IllegalArgumentException("The keyword argument is required");
        EntityManager em = KeyWord.entityManager();
        TypedQuery<KeyWord> q = em.createQuery("SELECT KeyWord FROM KeyWord AS keyword WHERE keyword.keyword = :keyword AND keyword.webProfile = :webprofile", KeyWord.class);
        q.setParameter("keyword", keyword);
        q.setParameter("webprofile", webprofile);
        return q;

Open in new window

It gives me error:
Parameter value [44] was not matching type [com.dissertation.webtools.domain.Url]; nested exception is java.lang.IllegalArgumentException: Parameter value [44] was not matching type [com.dissertation.webtools.domain.Url]

It happened when I added
 AND keyword.webProfile = :webprofile to the end of query statement
 q.setParameter("webprofile", webprofile);
Question by:static86
  • 2
  • 2

Expert Comment

ID: 35430931
Guessing form your code I assume both fields (keyword.keyword and keyword.webProfile) are strings. So, simply put them into quotes (the following is for MSSQL, you may have to adapt it to your DB system):

TypedQuery<KeyWord> q = em.createQuery("SELECT KeyWord FROM KeyWord AS keyword WHERE keyword.keyword = ':keyword' AND keyword.webProfile = ':webprofile'", KeyWord.class);

Open in new window

The difference are the ' signs around the parameters - they mark the contents to be strings.

BTW: You should include some details like the database in use (oracle, MSSQL, MySQL) as well as informations about the database fields. Without that, the solution may not meet your problem exactly.
LVL 13

Expert Comment

ID: 35430956
What is the datatype of the field in question?

Author Comment

ID: 35430958
I'm using mysql with hybernate.
Query SELECT KeyWord FROM KeyWord AS keyword WHERE keyword.keyword = :keyword works fine, but when I want to narrow it down by using AND keyword.webProfile = :webprofile it crashes.
I think that :webprofile and :keyword should not be quoted. These are variables which are defined by:
q.setParameter("keyword", SOME_JAVA_STRING_VARIABLE_HERE);
q.setParameter("webprofile", SOME_JAVA_STRING_VARIABLE_HERE);

Author Comment

ID: 35430966
webprofile is bigint(20)

Accepted Solution

Gnarf earned 2000 total points
ID: 35431373
If the web profile is an integer value you might consider to change the type of your variable webprofile to be an integer. This could be the simple solution, as a different setParameter may be called (the one with the integer parameter) that handles the value correctly.


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question