2

I know that there is a lot of similar questions here, but I've spent all day and didn't find a solution.

I'm using Hibernate 5.0.12 with PostgreSQL 9.6.3 in Spring Boot 1.5.4 project.

I have a simple user entity:

import javax.persistence.*;

@Entity
public class User {

    @Id
    @GeneratedValue
    @Column(nullable = false, updatable = false)
    private Long id;

    private String email;
    private String password;

    public Long getId() {       return id;  }
    public void setId(Long id)  {       this.id = id;   }
    public String getEmail() {      return email;   }
    public void setEmail(String email) {        this.email = email; }
    public String getPassword() {       return password;    }
    public void setPassword(String password) {      this.password = password; }
}

and application.properties:

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/my-db
spring.datasource.username=postgres
spring.datasource.password=postgres

spring.hibernate.dialect=org.hibernate.dialect.PostgreSQL94Dialect
spring.hibernate.hbm2ddl.auto=create

logging.level.org.hibernate=DEBUG

Launching looks normal

hibernate logs

but table "user" hasn't been created.

With MySQL all works fine. Is there any issue with PostgreSQL? I'm new in it and just created DB like that:

createdb -h localhost -p 5432 -U postgres my-db password *********

and, after some tries to fix the issue, created schema in it:

CREATE SCHEMA "my-db" AUTHORIZATION postgres;
2
  • DB from logs is company-crm url=jdbc:postgresql://localhost:5432/company-crm. Are you looking at the right DB? Commented Jun 13, 2017 at 14:01
  • @IssamEL-ATIF I've just replaced my company name to "company" in hibernate logs. There is 'company-crm' instead of 'my-db' in configs. It's the single base on my PC and connect works fine. Thanks for reply Commented Jun 13, 2017 at 14:09

2 Answers 2

7

user is a reserved word in PostgreSQL. Use different table name, like users and put @Table(name = "users") annotation on top of your entity class.

Sign up to request clarification or add additional context in comments.

3 Comments

I've changed table name. With "user" i got column user0_.id does not exist when tried to read from DB. Now I'm getting relation "users" does not exist. So, this was part of the problem, but the table is still not being created.
I would drop all the tables and see again, and in general for local usage create-drop seems to be better idea, but can't tell you for sure. I can only suggest to write create table statements by yourself and use Flyway to execute them.
There is nothing to drop in my case, it's the first try to use postgreSQL in a new project. I think, there is something wrong with my environment. Thanks for Flyway, we'll use something like this.
-1

As per your above code looks like you are missing @Table( name = "user" ) annotation.Can you please try once after adding this if that works?

2 Comments

May I know please why it is marked as negative vote? by which I can correct myself in future while giving answers of any question.
Hibernate creates table even without @Table annotation, This couldn't be the solution. Even if it is @Table( name = "user" ) doesn't work as user is reserved keyword in Postgre

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.