hql语法错误

aij0ehis  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(359)

MySQL5.1版本
休眠4.3
Spring4
玻璃鱼4
我用spring和hibernate编写了一个应用程序。它给出了以下错误。

ERROR:   You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM Account WHERE accountNo = '123'' at line 1

警告:standardwrappervalve[dispatcher]:servlet dispatcher的servlet.service()引发异常

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM Account WHERE accountNo = '123'' at line 1

我认为hql声明没有问题。
@repository中的方法

public Account getAccountByNo(String accountNo) {

        Account acc = null;

        Query query = getSession().createSQLQuery(" FROM Account WHERE accountNo = :accno ");
        query.setString("accno", accountNo);
        List list = query.list();

        if(!list.isEmpty()){
            acc = (Account)list.get(0);
        }

        return acc;
    }

    @Entity
    public class Account implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;

        @Column(name = "ACCOUNTNO", nullable = false)
        private String accountNo;
        @Column(name = "AMOUNT", nullable = false)
        private Double amount;

        @OneToOne
        private AccHolder accHolder;

    }
wixjitnu

wixjitnu1#

不创建hql查询。您可以创建sql查询:

Query query = getSession().createSQLQuery(" FROM Account WHERE accountNo = :accno ");

把它改成

Query query = getSession().createQuery(" FROM Account a WHERE a.accountNo = :accno ");

相关问题