HQL语法错误

HQL syntax error

我已经使用 spring 和休眠编写了一个应用程序。它给出以下错误。

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.service() for servlet dispatcher throw exception

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;

    }

您没有创建 HQL 查询。您创建一个 SQL-Query:

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

改为

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