File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate Query Language where clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Query Language where clause " Watch "Hibernate Query Language where clause " New topic
Author

Hibernate Query Language where clause

hari ann
Greenhorn

Joined: Dec 09, 2007
Posts: 11
Hi

I am having below query in my program. I am getting the error while running this


SQL Error: 1054, SQLState: 42S22
Unknown column 'usid' in 'where clause'

DynaValidatorActionForm myform=(DynaValidatorActionForm) form;
String usid=(String) myform.get("userid");
String qry="Select password from User as user where user.userid=usid";
Query query = session.createQuery(qry);
List l= query.list();
System.out.println(l.get(0));



<tr>
<td >User ID*</td>
<td><html:text property="userid"/>/td>
</tr>
<tr>
<td >Password*</td>
<td><html:password property="password" /></td>
/tr>



public class User {

private long userNo;
private String userid;
private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;


public String getUserid() {
return userid;
}

public void setUserid(String userid) {
this.userid = userid;
}

public String getPassWord(String userid){
return password;
}
}


<hibernate-mapping>
<class name="com.example.User" table="user">
<id column="uno" name="userNo">
<generator class="increment"/>
</id>
<property column="uid" name="userid"/>
<property column="upwd" name="password"/>
<
</hibernate-mapping>
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374

usid is a variable. It can't be used like this.
I ain't sure but try,

or
hari ann
Greenhorn

Joined: Dec 09, 2007
Posts: 11
I tried with the below one. But this time i am getting diffrent type of exception. Can you please help on this

org.hibernate.QueryException: Not all named parameters have been set: [uid] [select us.password from User us where us.userid = :uid]s

User user =(User) factory.getBean("user");
String uid=(String)form.get("userid");
String hql="select us.password from User us where us.userid = :uid";
Query query=session.createQuery(hql);
String dbpwd = (String) query.uniqueResult();
if(dbpwd.equals(uid)){
System.out.println(dbpwd);
return true;
}
else
{
return false;
}
}
Thirumurugan Sivaji
Greenhorn

Joined: Mar 03, 2009
Posts: 20
Hi,

Remodify your code as follows,

User user =(User) factory.getBean("user");
String hql="select us.password from User us where us.userid = :uid";
Query query=session.createQuery(hql);
query.setString("uid",(String)form.get("userid"));

or

User user =(User) factory.getBean("user");
String hql="select us.password from User us where us.userid = ?";
Query query=session.createQuery(hql);
query.setString(0,(String)form.get("userid"));

Warm Regards,

Thirumurugan Sivaji

hari ann
Greenhorn

Joined: Dec 09, 2007
Posts: 11
Thank you to all for your help

It has worked now.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
You're Welcome Susmita!
 
 
subject: Hibernate Query Language where clause