-1

I can't get rid of this error in any way. I want to create a table using Hibernate. But it gives me this error that there is no such table. But after all, he must create it, since it does not exist. I will be glad for any hint.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [order]
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [order]
aused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [order]

I use application.yaml

spring:
  jpa:
    hibernate:
      ddl-auto: validate
    show-sql: true
  datasource:
    url: jdbc:postgresql://localhost:5432/shop
    username: postgres
    password: 1111
  flyway:
    baseline-on-migrate: true

Also here is my code pom.xml. I heard that this error may be related to flywaydb, but I didn't find exactly how to solve it

<?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.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>TestTaskShop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>TestTaskShop</name>
    <description>TestTaskShop</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-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity6</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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
Ron
  • 1
  • 2
  • Please try this https://stackoverflow.com/questions/26881739/unable-to-get-spring-boot-to-automatically-create-database-schema/26882135#26882135 . If you fail please add your application.properties and pom to the question. – John Williams Mar 26 '23 at 08:12
  • Have a look at https://stackoverflow.com/questions/26881739/unable-to-get-spring-boot-to-automatically-create-database-schema/26882135#26882135 , specifically spring.jpa.hibernate.ddl-auto = create – John Williams Mar 26 '23 at 08:17

1 Answers1

0

If you want to create table from the Hibernate, you should pass hibernate properties in application.properties files.

spring.datasource.url=jdbc:mysql://localhost:3306/local
spring.datasource.username=root
spring.datasource.password=welcome
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.flyway.validate-on-migrate=false
  1. If you want create new table from entity you defined you can use create property of spring.jpa.hibernate.ddl-auto e.g spring.jpa.hibernate.ddl-auto=create

  2. For updating/alter table use update e.g spring.jpa.hibernate.ddl-auto=update

  3. For validating all tables are created use e.g spring.jpa.hibernate.ddl-auto=validate

  4. If you want to create and drop tables on application start up e.g spring.jpa.hibernate.ddl-auto=create-drop

Uttam Pawar
  • 104
  • 7