Search code examples
javamysqlspringhibernatejpa

i have this exception "org.hibernate.tool.schema.spi.CommandAcceptanceException:


this is my code

package com.examplesector.choosesectors.models.Entity;

import lombok.*;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Objects;

@Entity
@Data
@Table(name = "person")
public class Person {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;
   private String name;

   @JoinTable(name = "person_sector",
   joinColumns = {@JoinColumn(name = "PERSON_ID")},
   inverseJoinColumns = {@JoinColumn(name = "SECTOR_ID")})
   @ManyToMany(cascade = {CascadeType.ALL})
   private List<Sector> sectors;
   private Boolean checkedAgreeToTerms;


  }




  package com.examplesector.choosesectors.models.Entity;

  import lombok.*;

  import javax.persistence.*;
  import javax.validation.constraints.Size;
  import java.util.List;
  import java.util.Objects;

  @Entity
  @Data
  @Table(name = "sector")
  public class Sector {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Long id;
     private String nameSector;
     private String group;

     @ManyToMany()
     @JoinColumn(name = "PERSON_ID")
     private List<Person> persons;


  }

enter code here

properties
server.port=8082
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/dbsector? 
useUnicode=true&serverTimezone=UTC&useSSL=true&verifyServerCertificate=false
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
#spring.jpa.properties.hibernate.id.new_generator_mappings=true
#spring.jpa.properties.hibernate.jdbc.lab.non_contextual_creation=true

this error org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table sector (id bigint not null, group varchar(255), name_sector varchar(255), primary key (id)) engine=InnoDB" via JDBC Statement

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table person_sector add constraint FK7jwl41bm5xj1cyypbdl8vn2gp foreign key (sector_id) references sector (id)" via JDBC Statement

Caused by: java.sql.SQLException: Failed to open the referenced table 'sector'

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table sector_persons add constraint FKk28lnwtjxuu7gxx4998v0ex1m foreign key (sector_id) references sector (id)" via JDBC Statement


Solution

  • Since you specified the joinColumn and the inverseJoinColumn in the person class,try removing the @JoinColumn in the Sector class.