-->
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.  [ 3 posts ] 
Author Message
 Post subject: Query.setParameter not working for me
PostPosted: Wed Sep 14, 2005 12:10 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
I wrote this little generic routine that doesn't work.

The parameters (i.e. HashMap) and the query String that is built below look perfect in the debugger, so I'm not sure what I'm doing wrong. I've also tried the setParameter(int position, Object val) version using "?" marks.

Code:
    public List get(String queryPrefix, HashMap restrictions, String querySuffix) throws ManagerException
    {
        Set set = restrictions.keySet();
        if (restrictions == null || set.size() < 1)
            return this.get(queryPrefix + " " + querySuffix);
       
        this.openSessionInternally();
        Object keys[] = set.toArray();
        String queryString=queryPrefix;
        for (int i=0; i < keys.length; i++)
        {
            if (i==0)
                queryString = queryString
                            + " where ";
            else
                queryString = queryString
                            + " and ";
           
            queryString = queryString
                        + keys[i]
                        + " = :"
                        + keys[i];
        }
        if (querySuffix != null)
            queryString = queryString
                        + " "
                        + querySuffix;
       
        Query query = session.createQuery(queryString);
        for (int i=0; i < keys.length; i++)
            query.setParameter((String)keys[i], restrictions.get(keys[i]));
       
        List result = query.list();
        this.closeSessionInternally();

        return result;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 12:57 pm 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Before I go into debugging your code, could you clarify what you mean by
Quote:
... little generic routine that doesn't work.

Does it produce an exception? Isn't anything executed? Something else?

If Hibernate generates a call to the database, could you provide the result of show_sql for a simple use case?

Simple question to exclude other sources of error: I assume

Code:
this.openSessionInternally();

opens a new session, that is put into the variable

Code:
session

and

Code:
this.closeSessionInternally();

closes that session?

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 14, 2005 2:43 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Hi, ErikFK. Thanks for responding.

Quote:
... little generic routine that doesn't work.

No exceptions. I walked through the debugger as well. I simply get 0 objects in the List. However, if I build/pass-in a static query string (at run-time), that is, a query string with "column = value" embedded in it, instead of a setParameter approach, I get objects from the database back

Code:
this.openSessionInternally();
this.closeSessionInternally();

Yes, the session is a variable in the class. Other than the fact, I get no exceptions, I can actually see the values of the "session" variable in the debugger.

Here is an excerpt of the SQL generated/displayed by Hibernate:
Code:
select trade0_.TRD_ID as TRD1_ from TRADE trade0_ where trade0_.SYS_ID=? and trade0_.TRD_ID=?


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