HQL语法错误

时间:2015-04-28 06:42:34

标签: java mysql spring hql hibernate-4.x

  • mysql 5.1
  • hibernate 4.3
  • spring 4
  • glassfish 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调度程序的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;

    }

1 个答案:

答案 0 :(得分:1)

您不创建HQL查询。您创建一个SQL查询:

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

将其更改为

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