org.springframework.orm.hibernate3.HibernateQueryException - HibernateTemplate
我在使用 HibernateTemplate 时遇到问题,我不知道哪里出了问题。我正在使用 Hibernate3 和 Tomcat6。 在我的 DAO 中,我有一些函数尝试使用字符串查询数据库,并且它们似乎工作正常。像这样
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;
}
}
然而,当我尝试使用整数查询时。就像:
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;
}
}
我收到这个错误:
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!=?]
我的实体似乎有必要的注释。由于它适用于第一个功能,我不明白为什么它不适用于第二个功能。
@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; } }
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; } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您的代码中有一个拼写错误。
在第一个函数中,您在第二个“from appuser”中使用“from AppUser”。
尝试将第二个查询更改为“from AppUser”。
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".
谢谢你的回答
我意识到我们必须使用命令类名称而不是在查询中使用表名称
ModuleCommand - 命令类名称
app_module - 表名称。
在豆中
thank you for your answer
I realised we have to use commandclass name instead of using the table name in the query
ModuleCommand - command class name
app_module - table name.
in bean