query syntax problem

Posted on 2011-04-20
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
    LVL 2

    Expert Comment

    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

    What is the datatype of the field in question?

    Author Comment

    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

    webprofile is bigint(20)
    LVL 2

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    This article describes some very basic things about SQL Server filegroups.
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now