2

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)
2
  • 1
    Please turn on SQL logging Commented Jul 20, 2022 at 13:48
  • i have this in application.properties : logging.level.org.hibernate.SQL=DEBUG Commented Jul 20, 2022 at 14:27

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

9 Comments

At first it was create, then i changed it to update, and both of them are not working.
on docker or non docker impleamtion or is it on both ?
The microservice is on local and postgres database is containerized
I think we have a misunderstanding and that's my fault hh. so to be clear this is the issue: I have one only postgres database in container and the connection to it is fine and i can see the database from th pgadmin dashboard, but when running the spring boot microservice no tables are added to the database.
Out of scope: Why are you using an old non supported Spring Boot version?
|

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.