SQLGrammarException:无法执行语句(PostgreSQL + Hibernate)

时间:2014-12-28 11:22:06

标签: java hibernate intellij-idea

我的数据库包含User类,在User类中具有相同的命名字段。

我仍然遇到@Column注释问题:Intellij IDEA强调列的名称。

DBTABLE

    CREATE TABLE "user"
(
  email character varying,
  login character varying NOT NULL,
  password character varying NOT NULL,
  name character varying NOT NULL,
  id_user numeric NOT NULL,
  CONSTRAINT user_pkey PRIMARY KEY (login, password, id_user),
  CONSTRAINT user_email_key UNIQUE (email)
)

为什么这个语法异常?我有相同的命名表

根本原因

org.postgresql.util.PSQLException: ERROR: syntax error at or near "User"
  Position: 13
    org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1927)
    org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:561)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:419)
    org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:365)
    org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:133)
    org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3067)
    org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3509)
    org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
    org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
    org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
    org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:286)
    org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
    org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
    org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
    org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
    org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    com.classes.UserDB.registerUser(UserDB.java:17)
    com.servlets.Registration.doPost(Registration.java:29)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

用户

package com.DB;

import javax.persistence.*;

@Entity
public class User {

    @Lob
    private String email;
    @Lob
    private String login;
    @Lob
    private String password;
    @Lob
    private String name;

    @Id
    @GeneratedValue
    private int id_user;

    public int getId_user() {
        return id_user;
    }

    public void setId_user(int id_user) {
        this.id_user = id_user;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User getUserFromBase(){
        return this;
    }
}

如何解决?

2 个答案:

答案 0 :(得分:2)

user是保留字,无法创建名称为user的表。

尝试在@Table(name="USER_TABLE")之后添加@Entity来更改表名。

抱歉没有仔细阅读。看来你已经有了一个名为“USER”的表。那是oracle的问题

答案 1 :(得分:0)

“user”是PostgreSQL中的保留字,对于表或列使用保留字通常不是一个好主意。

如果你想节省很多麻烦,请使用其他名称。 users,user_acount。 也会改变与表相同的类名。

把@Table(name =“USERS”)注释:

@Entity
@Table(name="users")
public class Users {

}