我遇到了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;
}
}
2 回答
看起来您的代码中有拼写错误 . 在第一个函数中,您在第二个“来自appuser”中使用“来自AppUser” . 尝试将第二个查询更改为“从AppUser” .
谢谢你的回答我意识到我们必须使用命令类名而不是在查询中使用表名
ModuleCommand - 命令类名app_module - 表名 .
在 beans