- mysql 5.1
- hibernate 4.3
- Spring 4
- 玻璃鱼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;
}
最佳答案
您没有创建 HQL 查询。您创建一个 SQL 查询:
Query query = getSession().createSQLQuery(" FROM Account WHERE accountNo = :accno ");
更改为
Query query = getSession().createQuery(" FROM Account a WHERE a.accountNo = :accno ");
关于java - HQL语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912102/