I am having a problem with HibernateTemplate and I don’t know where I am going wrong. I am using Hibernate3 and Tomcat6.
In my DAO, I have functions that try to query the database using a string and they seem to work fine. Like this
public AppUser getUserByUserName(String username){
HibernateTemplate template=new HibernateTemplate(sessionFactory);
try{
List<AppUser> appList = template.find(" from AppUser as au where au.username=?", username);
tempUser = appList.get(0);
return tempUser;
} catch(Exception e){
System.out.println("Problem in AppUserDao--get byUsername: " + e.toString());
return null;
}
}
Yet, when I try to query using an integer. Like:
public List<AppUser> getAllMerchants(){
HibernateTemplate template=new HibernateTemplate(sessionFactory);
try{
List<AppUser> appList = template.find(" from appuser as au where au.securityLevel!=?", 112);
if(appList.size() > 0)
return appList;
else
return null;
} catch(Exception e){
System.out.println("Problem in AppUserDao--getAllMerchants: " + e.toString());
return null;
}
}
I get this error:
org.springframework.orm.hibernate3.HibernateQueryException: appuser is not mapped [ from appuser as au where au.securityLevel!=?]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: appuser is not mapped [ from appuser as au where au.securityLevel!=?]
My entity seems to have the necessary annotations. Since it works for the first function, I don’t understand why it doesn’t work for the second.
@Entity
@Table(name="appuser")
public class AppUser {
private int id;
private String username;
private String password;
private String firstName;
private String secondName;
private int state;
private int securityLevel;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator= "idSeq")
@SequenceGenerator(name="idSeq",sequenceName="app_user_seq_id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Column(name="password_2")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public int getSecurityLevel() {
return securityLevel;
}
public void setSecurityLevel(int securityLevel) {
this.securityLevel = securityLevel;
}
}
Looks like you have a typo in your code.
In the first function you use “from AppUser ” in the second “from appuser”.
Try to change the second query to “from AppUser”.