2015-04-28 60 views
0
  • MySQL的5.1
  • 冬眠4.3
  • 彈簧4
  • 的glassfish 4

我已經使用彈簧編寫的應用和休眠。它給出了以下錯誤。HQL語法錯誤

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 [調度員]:Servlet.service()進行servlet調度拋出異常

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

不創建一個HQL查詢。你創建一個SQL查詢:

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

將其更改爲

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