query syntax problem

Hi,
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
and
 q.setParameter("webprofile", webprofile);
static86Asked:
Who is Participating?
 
GnarfConnect With a Mentor Commented:
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.

HTH
Gnarf
0
 
GnarfCommented:
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.
0
 
sameer2010Commented:
What is the datatype of the field in question?
0
 
static86Author Commented:
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);
0
 
static86Author Commented:
webprofile is bigint(20)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.