java org.hibernate.exception.SQLGrammarException: 无法再次执行查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11322706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.exception.SQLGrammarException: could not execute query again
提问by Sudeepta
I am new in Spring and Hibernate. I am trying to insert and retrieve all data from database. I have inserted data in mySql db and now i am trying to retrieve all data from database but i am getting some exception. What i can do to fix the problem.
我是 Spring 和 Hibernate 的新手。我正在尝试从数据库中插入和检索所有数据。我已经在 mySql db 中插入了数据,现在我试图从数据库中检索所有数据,但出现了一些异常。我能做些什么来解决这个问题。
Jul 04, 2012 10:46:58 AM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1064, SQLState: 42000
Jul 04, 2012 10:46:58 AM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: 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 ') as emp1_0_, employee0_.address as address0_, employee0_.emp_email as emp3_0_, ' at line 1
Hibernate: select employee0_.emp_id) as emp1_0_, employee0_.address as address0_, employee0_.emp_email as emp3_0_, employee0_.manager_id as manager4_0_, employee0_.emp_name as emp5_0_ from employee employee0_
org.hibernate.exception.SQLGrammarException: could not execute query
Hibernate code is
休眠代码是
public void getAll() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = (Transaction) session.beginTransaction();
List employees = session.createQuery("from Employee").list();
for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
Employee employee = (Employee) iterator.next();
System.out.println(employee.getName());
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
} finally {
session.close();
}
}
Employee.java
雇员.java
@Entity
@Table(name="employee")
public class Employee {
private int id;
private String name;
private String email;
private String address;
private int managerId;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="emp_id)")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="emp_name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="emp_email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Column(name="manager_id")
public int getManagerId() {
return managerId;
}
public void setManagerId(int managerId) {
this.managerId = managerId;
}
}
回答by Pramod Kumar
this
这
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="emp_id)")
public int getId() {
return id;
}
should be -
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="emp_id")
public int getId() {
return id;
}
Remove )
from the end of name="emp_id)"
)
从末尾删除name="emp_id)"
回答by Sunil Chavan
There is typo error in the Id field emp_id, having extra ')'
Id 字段 emp_id 中存在拼写错误,有额外的 ')'
@Column(name="emp_id)")
Remove the curly bracket it will work.
取下大括号它将起作用。
回答by Hemant Metalia
column name for id you specified is containing ) remove it e.g.
您指定的 id 的列名包含)将其删除,例如
replace
@Column(name="emp_id)") with @Column(name="emp_id")
代替
@Column(name="emp_id)") with @Column(name="emp_id")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="emp_id")