kawlorridor
kawlorridor

Reputation: 23

Hibernate Jpa Error: "java.sql.SQLSyntaxErrorException: Table doesn't exist"

Somehow i am getting that Caused by: java.sql.SQLSyntaxErrorException: Table 'zahnmobil.worker_table' doesn't exist. I have tried everything i found everywhere but NOTHING has worked. Help would be highly appreciated !! My Pom.xml file dependencies:

<description>Mobile Zahnarzt System.</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.adesso.zahnmobil.web</groupId>
            <artifactId>web-generated</artifactId>
            <version>1.0.0</version>
        </dependency>
           

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

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

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>

My Entitiy:

package com.adesso.zahnmobil.model;

import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.transaction.annotation.Transactional;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table(name="worker_table")

public class Worker {
  @Id
  private String id = UUID.randomUUID().toString();

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

Repository:

package com.adesso.zahnmobil.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.adesso.zahnmobil.model.Worker;
public interface WorkerRepository extends JpaRepository<Worker, String> {
    
}

Application.yml file

server:
  hostname: ${SERVER_HOSTNAME:http://localhost:4200}
  forward-headers-strategy: native
  servlet:
    session:
      cookie:
        http-only: true
        secure: true

  error:
    include-message: always
    include-binding-errors: always

spring:
  datasource:
    username: root
    password: password
    url: jdbc:mysql://${DATABASE_HOST:localhost}:${SQL_PORT:3306}/${DATABASE_NAME:zahnmobil}?createDatabaseIfNotExist=true&useSSL=false

testDatabase:
  enabled: ${DATABASE_POPULATE_WITH_TESTOBJECTS:true}

jpa:
  database-platform: ${SQL_DIALECT:org.hibernate.dialect.MySQL8Dialect}
  generate-ddl: true
  hibernate:
    ddl-auto: ${SQL_DDL_AUTO:create-drop}
  show-sql: true

My SpringbootApplciation File

package com.adesso.zahnmobil;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;


@SpringBootApplication
@EntityScan("com.adesso.zahnmobil.model") 
public class ZahnmobilApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZahnmobilApplication.class, args);
    }

}

I am completly lost. I have tried the Entitiy scan but its still not working. I really dont know what to do as i have already tried everything i found on the Internet

Upvotes: 0

Views: 207

Answers (1)

zawarudo
zawarudo

Reputation: 2506

Based on your pom, I assume that you are using the latest version of Spring Boot which is 3. With that, in mind one of the major changes that happened between Spring Boot 2 and 3 was a migration of packages from javax to jakarta. That means that all imports in your project should have jakarta instead of javax. Like in your entity instead of

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

you should have

import jakarta.persistence.*;

Also I see that you added for some reason a lot of duplicate dependencies like from last one

    <dependency> //you already have starter test defined on top
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency> //do you need security for current debugging task?
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency> //do you need security for current debugging task?
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
    </dependency>

    <dependency> //core is already included in the starter
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
    </dependency>

    <dependency> //you already defined jpa on top
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
    </dependency>

     <dependency> //you already have mysql dependency
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

If you are just creating a project try to keep dependencies minimal until you resolve your current issue with something like

<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>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

In order for changes to be visible you will need to Maven > Update / Reload in your IDE

After you resolve that issue you can keep adding dependencies that you need, but I strongly believe that jakarta imports will fix the issue.

Upvotes: 0

Related Questions