-->
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: Frustrated - can't get queries to work for some reason
PostPosted: Mon Mar 07, 2005 5:35 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 6:04 pm
Posts: 23
Hello all.

I've had queries working in my hibernate projects before when I used it about 6 months ago. For some reason now I'm always getting the same exception regardless which (of the 2) classes I try to query on now. I'm hoping somebody will have some insight to point me in the right direction.

I can successfully load() an object, even update() an object, but I cannot run a query. I have hibernate2.jar installed in my jboss default app's lib folder (and its dependencies as far as i can tell).

Here is the code that executes the query...

Code:
            Query q = session.createQuery("select u from Users as u where u.userName = 'somename'");
            q.setMaxResults(10);
            for (Iterator it = q.iterate(); it.hasNext();)
            {
                UserTO user = (UserTO) it.next();
            }


Here is the exception that is thrown...

Message : 16:14:10,015 INFO [STDOUT] unexpected token: as [select u from Users as u where u.userName = 'somename']

If you need the rest of the stack trace let me know (it's long so I don't want to clutter this message).

Here is a snip from my mapping file...

Code:
<hibernate-mapping>
  <class name="qnext.bo.database.to.UserTO" table="users" dynamic-update="true" lazy="true">
    <id column="UID" name="UID" type="java.lang.Long">
      <generator class="native"/>
    </id>
    <property column="username" length="50" name="userName" type="java.lang.String"/>
    <property column="password" length="50" name="password" type="java.lang.String"/>
    <property column="detectedip" length="15" name="detectedIP" type="java.lang.String"/>
    <property column="ip" length="15" name="reportedIP" type="java.lang.String"/>
    <property column="PROXY" length="1" name="proxy" type="java.lang.String"/>
    <property column="proxylist" length="16277215" name="proxyList" type="java.lang.String"/>
    <property column="PRIVATEID" length="11" name="privateID" type="java.lang.Integer"/>
    <property column="SESSIONID" length="11" name="sessionID" type="java.lang.Integer"/>


I hope you can see what I'm doing wrong. I'm going batty! I may end up getting the source code to hibernate to solve this, but this seems like overkill to me.

Thanks,

Mal.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 7:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Query q = session.createQuery("select u from qnext.bo.database.to.UserTO as u where u.userName = 'somename'");


Note: In most cases, its better to use parameter arguments.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 1:14 am 
Beginner
Beginner

Joined: Thu Feb 17, 2005 6:04 pm
Posts: 23
Shoot! I haven't tried it but I'm almost sure you're right. I forgot to fully qualify the object name. Perhaps I should remove the package information from the mapping file. It makes the queries a little ugly.

Thanks.

Mal.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 1:50 am 
Beginner
Beginner

Joined: Thu Feb 17, 2005 6:04 pm
Posts: 23
Yep, that was it. It was obviously my mistake, and an obvious one, but I feel the error message could have helped point me in the right direction a little more. Something along the lines of "could not create object XX", etc.

Mal.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 8:29 am 
Newbie

Joined: Wed Feb 23, 2005 9:00 am
Posts: 8
instead of using ='somename' use =\'somename\' in Query


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 11:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Its not a matter of fully qualified name or not. You were attempting to write a query against the table name, not the class name.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 8:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You always have to option of using Java itself to help avoid these issues.
eg
Code:
Query q = session.createQuery("select u from "+ UserTO.class.getName() + " as u where u.userName = 'somename'");


So the compiler can catch the error if it does not exist.


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.