0

I'm trying to make a project in Spring. I want to use Hibernate to generate my tables. Unfortunately, Hibernate doesn't create my tables for some reasons that I can't seem to find. I would like for it to create my tables according to my entities.

Here are my entities:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Book {
    @Id
    private Long id;
    @Column(name = "title")
    private String title;
    @Column(name = "author")
    private String author;
    @Column(name = "description")
    private String description;
    @Column(name = "release_year")
    private Integer releaseYear;
    @Column(name = "rentalRate")
    private Integer rentalRate;
    @Column(name = "pages")
    private Integer pages;
    @Column(name = "replacement_cost")
    private Float replacementCost;
    @Column(name = "rating")
    private Float rating;

    @ManyToOne
    @JoinColumn(name="languageId", nullable=false)
    private Language language;

}

and:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name="Language")
public class Language {
    @Id
    private Long languageId;
    @Column(name = "name")
    private String name;
    @Column(name = "last_updated")
    private String lastUpdated;

    @OneToMany(mappedBy = "language")
    private Set<Book> books;
}

Also, here is my POM file:

<?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>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>ro.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--Hibernate-->

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.27.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>


        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>7.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

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

And my app properties:

server.port=8080
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=practice_user
spring.datasource.password=practice_user
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.show-sql=true
spring.flyway.enabled=false
spring.jpa.hibernate.ddl-auto=update
spring.security.user.name=admin
spring.security.user.password=admin
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

Things that I already tried:

  • changing spring.jpa.hibernate.ddl-auto=create
  • changing spring.jpa.database-platform=Postgres

1 Answers1

1

the following application.yml file works:

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=practice_user
spring.datasource.password=practice_user
spring.datasource.hikari.schema=public
spring.jpa.generate-ddl = true

you can replace the schema name if its not set to public by default.

Also, add the @Table(name = "BOOK") annotation to your book entity

Padua
  • 57
  • 2
  • 12
  • -added the proposed lines in the application properties -i made sure that the schema name is "public" -i added the @Table annotation to the entity It still doesn't generate the tables – Damian Narcis Aug 06 '21 at 20:28
  • if you have made any changes to the pom file, did you run maven clean install afterwards ? also, do you have your postgres server up and running? – Padua Aug 06 '21 at 20:32
  • yes sir, i did run maven clean install – Damian Narcis Aug 06 '21 at 20:33
  • 1
    In springboot 2.3.0 ( I upgraded from 2.2.x version ) when I added `spring.jpa.generate-ddl = true` in the application.configuration the tables were created – darkman97i Feb 02 '22 at 10:26