Mena_Mena
Mena_Mena

Reputation: 253

Spring Data JPA could not create tables in database

i'm creating a Restful API for Stock Management, i've created my Entities and filled the application.properties but when i run my app to create the db and the tables, only the db is created, and nothing is shown as error.

application.properties:

server.port=8097
spring.datasource.url=jdbc:mysql://localhost:3306/exercice_db?serverTimezone=UTC&createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=create

Application.java:

package org.sid;

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

@SpringBootApplication
public class ProjectAppServerApplication {

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

}

Produit.java :

//imports

@Entity
@Table(name = "produit")
public class Produit {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long idProduit;
    
    @Size(min=3, max=1000)
    public String Desprod;
    
    @Size(min=3, max=1000)
    public String Catprod;
    
    @OneToMany(mappedBy="produit", cascade=CascadeType.ALL)
    Set<Acheter> acheter = new HashSet();

    public Produit() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Produit(String desprod, String catprod) {
        super();
        this.Desprod = desprod;
        this.Catprod = catprod;
    }

    //Getters and Setters
        
    
}

Fournisseur.java :


//Imports

@Entity
@Table(name = "fournisseur")
public class Fournisseur {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long idFRS;

    @Size(min=3, max=1000)
    public String nomFRS;

    public String VillFRS;

    @OneToMany(mappedBy="fournisseur", cascade=CascadeType.ALL)
    Set<Acheter> acheter = new HashSet();
    
    public Fournisseur() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Fournisseur(String nomFRS, String villFRS) {
        super();
        this.nomFRS = nomFRS;
        this.VillFRS = villFRS;
    }

    //Getters and Setters
    
}

Acheter.java :

//Imports

@Entity
@Table(name = "Acheter")
public class Acheter {
    
    @ManyToOne
    @JoinColumn(name ="idProduit")
    Produit produit;
    
    @ManyToOne
    @JoinColumn(name ="idFRS")
    Fournisseur fournisseur;
    
    public String prixAchat;

    public Acheter() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Acheter(Produit produit, Fournisseur fournisseur, String prixAchat) {
        super();
        this.produit = produit;
        this.fournisseur = fournisseur;
        this.prixAchat = prixAchat;
    }

    //Getters and Setters
    
}

Any ideas what is wrong?

Upvotes: 1

Views: 1113

Answers (2)

Godwin Oziegbe
Godwin Oziegbe

Reputation: 1

Change the spring boot version to an earlier version. I changed mine from version 2.7.1 to 2.6.2 in the pom xml file and it worked!

Upvotes: 0

Jalloul95
Jalloul95

Reputation: 216

Try using CompositeKey : https://www.baeldung.com/jpa-many-to-many

AcheterKey.java

@Embeddable
public class AcheterKey implements Serializable {

    @Column(name = "idProduit")
    Long produitId;

    @Column(name = "idFrs")
    Long frsId;
}

Acheter.java

@Entity
@Table(name = "acheter")
public class Acheter {
    
    @EmbeddedId
    AcheterKey id;
    
    @ManyToOne
    @MapsId("produitId")
    @JoinColumn(name ="idProduit")
    Produit produit;
    
    
    @ManyToOne
    @MapsId("frsId")
    @JoinColumn(name ="idFrs")
    Fournisseur fournisseur;
    
    public String prixAchat;
  
    //Constructor, Gettes and Setters
}

Upvotes: 1

Related Questions