org.springframework.orm.hibernate3.HibernateQueryException - HibernateTemplate

发布于 2024-10-15 09:08:54 字数 2945 浏览 6 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2024-10-22 09:08:54

看起来您的代码中有一个拼写错误。
在第一个函数中,您在第二个“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".

梓梦 2024-10-22 09:08:54

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

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

ModuleCommand - 命令类名称
app_module - 表名称。

在豆中

<bean id="....
.
.
.
<property name="commandClass" value="com.web.mon.thread.ModuleCommand" />
...
</bean>

thank you for your answer
I realised we have to use commandclass name instead of using the table name in the query

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

ModuleCommand - command class name
app_module - table name.

in bean

<bean id="....
.
.
.
<property name="commandClass" value="com.web.mon.thread.ModuleCommand" />
...
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文