本文介绍了休眠:奇怪的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题:我有一个带有id属性的简单实体,我无法保存它。
线程main中的异常org.hibernate.exception.SQLGrammarException:无法执行语句
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at ...
导致:org.postgresql.util.PSQLException:错误:语法错误处于或接近用户
位置: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();
code $ <$ $ p
$ bHQL: Hibernate:create table User(userId int8 not null,主键(userId))
Hibernate:创建序列hibernate_sequence $ b hibernate_sequence
$ pre $$ b INFO:HHH000230:模式导出完成
Hibernate:select nextval('hibernate_sequence')
Hibernate:插入User(userId)的值(?)
WARN:SQL错误:0,SQLState :42601
错误:错误:在用户或其附近出现语法错误
位置:13
解决方案用户是许多数据库中的保留关键字。
您不应该这样命名表。
试试:
@Entity
@Table(name = MyUser)
public class User {
@Id
@GeneratedValue
private long userId;
Problem: I've a simple entity with id property, and I can't save it.
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 ...
Code:
@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 is a reserved Keyword in many Databases.
You should not name your table like this.
Try:
@Entity @Table(name = "MyUser") public class User { @Id @GeneratedValue private long userId;
这篇关于休眠:奇怪的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!