Syntax error when creating table in PostgreSQL using Hibernate

4
    @Entity
public class User {
    @Id
    @GeneratedValue
    private int id;
    private String name;
    private String email;
    private String sex;
    private int age;
    private Date birthday;
    private Date registerdate;
    private String login;
    private String password;
    @ManyToMany
    @JoinTable(name = "eventlike_user", joinColumns = @JoinColumn(name = "id_user"), inverseJoinColumns = @JoinColumn(name = "id_eventlike"))
    private Collection<Eventlike> eventlikes;
    @OneToMany
    private Collection<Event> events;

   //getters and setters

I get the following error when creating tables:

  

ERROR: HHH000388: Unsuccessful: create table User (id int8 not null,   birthday timestamp, email varchar (255), login varchar (255), name   varchar (255), password varchar (255), registerdate timestamp, sex   varchar (255), primary key (id)) Nov 12, 2014 10:55:33 AM   org.hibernate.tool.hbm2ddl.SchemaUpdate Run ERROR: ERROR: Error   syntax at or near "User" Position: 14

I do not know what is causing this table to be created, but what catches my attention is this type int8 of id .

    
asked by anonymous 12.11.2014 / 15:20

2 answers

4

This error occurs because accented characters, capital letters, and reserved words should must be escaped with quotation marks " . In your case user is a reserved word. As this SOen response your annotation should look like this:

For JPA 1.0

@Entity
@Table(name="'User'")
public class User {
    ...
}

JPA 2.0

@Entity
@Table(name="\"User\"")
public class User {
    ...
}
    
12.11.2014 / 18:20
2

PostgreSQL has as a convention to use lowercase characters to represent identifiers, that is, object names (schemas, tables, views, functions, procedures). It is good practice to follow this.

When we declare objects with capital letters, we have to use quotation marks. For example: create table "User" .

As far as I've researched, Hibernate does not treat these differences transparently. It may even have some parameters.

You can try adding the @Table annotation to your class by specifying the name in lowercase. Example:

@Entity
@Table(name="user")
public class User { 
    ...
}

In addition to this change, make sure the table already exists in the database and, if so, normalize its name to lowercase. Do this for all tables.

    
12.11.2014 / 18:16