2

我在使用 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 未映射 [ from appuser as au where au.securityLevel!=?]; 嵌套异常是 org.hibernate.hql.ast.QuerySyntaxException: appuser is not mapped [ from appuser as au where au.securityLevel!=?]

我的实体似乎有必要的注释。由于它适用于第一个功能,我不明白为什么它不适用于第二个功能。

@实体
@Table(name="appuser")
公共类 AppUser {
    私人 int id;
    私人字符串用户名;
    私人字符串密码;
    私人字符串名;
    私人字符串第二名;
    私有 int 状态;
    私有 int 安全级别;

    @ID
    @GeneratedValue(策略= GenerationType.AUTO,生成器=“idSeq”)
    @SequenceGenerator(name="idSeq",sequenceName="app_user_seq_id")
    公共 int getId() {
        返回标识;
    }
    公共无效setId(int id){
        这个.id = id;
    }
    公共字符串 getUsername() {
        返回用户名;
    }
    公共无效 setUsername(字符串用户名){
        this.username = 用户名;
    }

    @Column(name="password_2")
    公共字符串 getPassword() {
        返回密码;
    }
    公共无效setPassword(字符串密码){
        this.password = 密码;
    }
    公共字符串 getFirstName() {
        返回名字;
    }
    公共无效setFirstName(字符串名字){
        this.firstName = 名字;
    }
    公共字符串 getSecondName() {
        返回第二个名字;
    }
    公共无效 setSecondName(字符串 secondName){
        this.secondName = secondName;
    }
    公共 int getState() {
        返回状态;
    }
    公共无效 setState(int state) {
        this.state = 状态;
    }
    公共 int getSecurityLevel() {
        返回安全级别;
    }
    公共无效 setSecurityLevel(int securityLevel) {
        this.securityLevel = 安全级别;
    }

}
4

2 回答 2

6

看起来您的代码中有错字。在第一个函数中,您在第二个“来自 appuser”中使用“来自 AppUser”。尝试将第二个查询更改为“来自 AppUser”。

于 2011-02-01T09:51:59.023 回答
1

谢谢你的回答我意识到我们必须使用命令类名称而不是在查询中使用表名

return getHibernateTemplate().find("from ModuleCommand order by application");

ModuleCommand - 命令类名 app_module - 表名。

在豆

<bean id="....
.
.
.
<property name="commandClass" value="com.web.mon.thread.ModuleCommand" />
...
</bean>
于 2012-05-08T11:00:55.403 回答