问题:我有一个具有 id 属性的简单实体,但无法保存它。
异常(exception):
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at ...
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "User"
Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
at ...
代码:
@Entity
@Table(name = "User")
public class User {
@Id
@GeneratedValue
private long userId;
//getter & setter
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
User user = new User();
session.persist(user);
session.getTransaction().commit();
session.close();
}
}
HQL:
Hibernate: create table User (userId int8 not null, primary key (userId))
Hibernate: create sequence hibernate_sequence
INFO: HHH000230: Schema export complete
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into User (userId) values (?)
WARN: SQL Error: 0, SQLState: 42601
ERROR: ERROR: syntax error at or near "User"
Position: 13
最佳答案
User 是许多数据库中的保留关键字。
您不应该这样命名您的表。
尝试:
@Entity
@Table(name = "MyUser")
public class User {
@Id
@GeneratedValue
private long userId;
关于java - Hibernate:奇怪的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22959373/