1

I am trying to generate tables based on entities. The application starts correctly, none of tables are created. What's wrong?

I'm using postgre sql version 14 and java 17

here is my pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.quizmenia</groupId>
    <artifactId>quizmeniaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>quizmeniaserver</name>
    <description>quizmenia - Application to host quiz for students, communities etc</description>
    <properties>
        <java.version>17</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/quizmenia
spring.datasource.username=postgres
spring.datasource.password=admin

spring.jpa.hibernate.ddl-auto=create-drop

spring.datasource.schema=public

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

spring.jpa.open-in-view=true

As you can see I have the spring.jpa.hibernate.ddl-auto=create-drop line added but the tables are still not being created by the JPA. What is wrong?

user entity:

package com.quizmenia.quizmeniaserver.model;

import java.util.*;
import javax.persistence.*;

import com.fasterxml.jackson.annotation.JsonIgnore;


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

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false)
    private Long userId;

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;

    @Column(name="email")
    private String email;

    @Column(name="password")
    private String password;

    @Column(name="enable")
    private Boolean enable;

    @Column(name="about")
    private String about;

    @Column(name="phone_number")
    private Integer phoneNumber;


    public user() {
        
    }



    public user(Long userId, String firstName, String lastName, String email, String password, Boolean enable,
            String about, Integer phoneNumber) {
        this.userId = userId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.enable = enable;
        this.about = about;
        this.phoneNumber = phoneNumber;
    }


    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    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;
    }

    public Boolean getenable() {
        return enable;
    }

    public void setenable(Boolean enable) {
        this.enable = enable;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }

    public Integer getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(Integer phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

}

folder structure:

enter image description here

1
  • probably schema is created and no update, drop the schema. Add logs to sql to look what is going on Commented May 18, 2023 at 22:08

2 Answers 2

1

Try change:

@Table(name = "users")

To:

@Table(name = "users",  schema = "public", catalog = "quizmenia")
Sign up to request clarification or add additional context in comments.

2 Comments

NO, after replacing it does not create tables in the DB
can you check in postgree that your schema is indeed under public quizmenia?
0

I think there is a problem in application.properties file You can find some useful things about hbm2ddl in What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

spring.datasource.url=jdbc:postgresql://localhost:5432/quizmenia
spring.datasource.username=postgres
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=create
spring.datasource.schema=public
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true  
spring.jpa.properties.hibernate.dialect=..
spring.jpa.open-in-view=true

Note: when show SQL is true at first check the queries that shows in the console.

2 Comments

that's the problem na I don't know which property to add or update, this is what is standard to connect PostgreSQL but it doesn't get any solution.
spring.jpa.hibernate.ddl-auto is the property that you must change . You set this property as create-drop but you must set it create only like this. spring.jpa.hibernate.ddl-auto=create

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.