chamikaWKK
chamikaWKK

Reputation: 21

HQL syntax error

I have written a application using spring and hibernate. It gives following error.

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

Warning: StandardWrapperValve[dispatcher]: Servlet.service() for servlet dispatcher threw 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

I think there is no issue with the hql statement.

method inside @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;

    }

Upvotes: 0

Views: 478

Answers (1)

Jens
Jens

Reputation: 69440

You do not create a HQL Query. Your create a SQL-Query:

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

Change it to

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

Upvotes: 1

Related Questions