Home > Software engineering >  spring boot is not creating table from entity using postgres container
spring boot is not creating table from entity using postgres container

Time:07-20

I'm trying to create a spring boot microservice with a postgres database, the connection to database is done but the tables from entities are not created. Anyone help me please! I have postgres in a container: enter image description here this is my application.yml:

server:
  port: 8080

spring:
  application:
    name: mpService

  datasource:
    url: jdbc:postgresql://localhost:5432/mpservice
    username: farah
    password: ****
    jpa:
      properties:
        hibernate:
          dialect: org.hibernate.dialect.PostgreSQLDialect
          format_sql: true
      hibernate:
        ddl-auto: update
      show-sql: true

This is the Entity :

import lombok.*;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

import javax.persistence.Entity;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;


@Builder
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@Entity
@Table(name = "projects")
public class Project implements Serializable {
    @Id
    @SequenceGenerator(
            name= "project_id_sequence",
            sequenceName = "project_id_sequence"
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "project_id_sequence"
    )
    Long idP;
    String nomP;
    @Enumerated(EnumType.STRING)
    CategoryP categoryP;
    String descriptionP;
    @Temporal(TemporalType.DATE)
    Date dateDebutP;
    @Temporal(TemporalType.DATE)
    Date dateFinP;
    double budget;
}

dependencies :

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

Docker-compose with which i created the containers:

services:
  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: farah
      POSTGRES_PASSWORD: ****
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:[email protected]}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"
    networks:
      - postgres
    restart: unless-stopped
networks:
  postgres:
    driver: bridge

volumes:
  postgres:
  pgadmin:

Log:

,------.  ,--.   ,--.              ,--.                                                           ,--.
|  .--. ' |   `.'   |   ,--,--,--. `--'  ,---. ,--.--.  ,---.   ,---.   ,---.  ,--.--. ,--.  ,--. `--'  ,---.  ,---.
|  '--' | |  |'.'|  |   |        | ,--. | .--' |  .--' | .-. | (  .-'  | .-. : |  .--'  \  `'  /  ,--. | .--' | .-. :
|  | --'  |  |   |  |   |  |  |  | |  | \ `--. |  |    ' '-' ' .-'  `) \   --. |  |      \    /   |  | \ `--. \   --.
`--'      `--'   `--'   `--`--`--' `--'  `---' `--'     `---'  `----'   `----' `--'       `--'    `--'  `---'  `----'
2022-07-20 13:53:52.790  INFO 30816 --- [           main] com.cra.MpServiceApplication             : Starting MpServiceApplication using Java 11.0.15 on DESKTOP-2DAHVVC with PID 30816 
2022-07-20 13:53:52.794  INFO 30816 --- [           main] com.cra.MpServiceApplication             : No active profile set, falling back to default profiles: default
2022-07-20 13:53:53.743  INFO 30816 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-07-20 13:53:53.829  INFO 30816 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 71 ms. Found 1 JPA repository interfaces.
2022-07-20 13:53:54.421  INFO 30816 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-07-20 13:53:54.430  INFO 30816 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-07-20 13:53:54.431  INFO 30816 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
2022-07-20 13:53:54.531  INFO 30816 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-07-20 13:53:54.531  INFO 30816 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1670 ms
2022-07-20 13:53:54.662  INFO 30816 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-07-20 13:53:54.834  INFO 30816 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-07-20 13:53:54.881  INFO 30816 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-07-20 13:53:54.928  INFO 30816 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.32.Final
2022-07-20 13:53:55.085  INFO 30816 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-07-20 13:53:55.252  INFO 30816 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2022-07-20 13:53:55.749  INFO 30816 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-07-20 13:53:55.761  INFO 30816 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-07-20 13:53:56.036  WARN 30816 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-07-20 13:53:56.347  INFO 30816 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-20 13:53:56.359  INFO 30816 --- [           main] com.cra.MpServiceApplication             : Started MpServiceApplication in 4.193 seconds (JVM running for 5.827)

CodePudding user response:

It took me a while because I hate yaml but it should it's wrong.

this is the correct file:

server:
  port: 8080

spring:
  application:
    name: mpService

  datasource:
    url: jdbc:postgresql://localhost:5432/mpservice
    username: farah
    password: ****

  jpa:
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    hibernate:
      ddl-auto: update
    show-sql: true

JPA is its own node it's not under datasource.

CodePudding user response:

Try this on you JPA config

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/mpservice
    username: farah
    password: ****
    platform: postgres
    initialization-mode: always
    continue-on-error: true
  jpa:
    show-sql: true
    generate-ddl: true
    hibernate:
      ddl-auto: auto
    database: postgresql
  • Related