0

Having an issue with spring boot table creation.

Have 3 entities, the booking and property having a ManytoOne relation with the user entity:

@Entity

public class Booking implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "uuid2")

    @GenericGenerator(name = "uuid2", strategy = "uuid2")

    @Type(type = "uuid-binary")

    private UUID booking_id;

    @Column(name = "startdate", nullable = false)

    private Time startdate;

    @Column(name = "enddate", nullable = false)
    private Time enddate;

    @ManyToOne
    @JoinColumn(name="property_id", nullable=false)
    private Property property;

    @ManyToOne
    @JoinColumn(name="tenant_id", nullable=false)
    private User user;

    @Column(name = "date_made", nullable = false)
    private Time date_made;
@Entity()

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id

    @GeneratedValue(generator = "uuid2")

    @GenericGenerator(name = "uuid2", strategy = "uuid2")

    @Type(type = "uuid-binary")

    private UUID user_id;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "birth_date", nullable = false)
    private String birth_date;

    @Column(name = "gender", nullable = false)
    private String gender;

    @Column(name = "age", nullable = false)
    private int age;

    @Column(name = "address", nullable = false)
    private String address;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "role", nullable = false)
    private int role;

    @OneToMany(mappedBy="user")
    private Set<Booking> bookingList;


    @OneToMany(mappedBy="user")
    private Set<Property> propertiesList;
 @Entity

public class Property implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id

    @GeneratedValue(generator = "uuid2")

    @GenericGenerator(name = "uuid2", strategy = "uuid2")

    @Type(type = "uuid-binary")

    private UUID property_id;

    @ManyToOne

    @JoinColumn(name="user_id", nullable=false)

    private User user;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "location", nullable = false)
    private String location;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "rentCpcty", nullable = false)
    private int rentCpcty;

When starting the application, with the spring.jpa.hibernate.ddl-auto = create/create-drop, out of the 3 entities, only booking a property are created as tables, although is the run console the following message is displayed:

Hibernate: create table booking (booking_id bytea not null, date_made time not null, enddate time not null, startdate time not null, property_id bytea not null, tenant_id bytea not null, primary key (booking_id))

Hibernate: create table property (property_id bytea not null, location varchar(255) not null, name varchar(255) not null, password varchar(255) not null, rent_cpcty int4 not null, user_id bytea not null, primary key (property_id))

Hibernate: create table user (user_id bytea not null, address varchar(255) not null, age int4 not null,

In pgAdmin, only the 2 tables are displayed.

To check if this is only a pgAdmin error I used POST or GET are used in Postman on the user adress, the following error is displayed:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause org.postgresql.util.PSQLException: ERROR: syntax error at or near "user"

My pom.xml:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rentBackEnd</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>backEnd</name>
<description>backEnd for final project</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator-annotation-processor</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.2.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>
</dependencies>
    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
   </build>
VedantK
  • 9,728
  • 7
  • 66
  • 71
AdrianR
  • 3
  • 3

1 Answers1

0

user is a keyword in SQL. Make it anything else other than user.

For eg 'users'

But if you want to create with same name then use Below.

@Table(name="\"user\"")
Alien
  • 15,141
  • 6
  • 37
  • 57