Hej, mam problem w moim basicowym rest api. Spring twierdzi że moja tabela istnieje i nie może utworzyć nowej. Co więcej, nie może jej zresetować, ponieważ ona od czegoś zależy. Problem pojawia się odkąd mój model zawiera pole, które jest stworzonym przeze mnie obiektem. Nagle dostaje ogrom błędów. Proszę o pomoc bo nawet na stackoverflow nie widzę odpowiedzi na mój problem. Dzięki z góry
import javax.persistence.*;
import java.time.LocalDate;
@Entity
@Table(name = "cassets_table")
public class VideoCasset {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;
private String title;
private LocalDate productionYear;
@ManyToOne
private Genre genre;
public VideoCasset() {
}
public VideoCasset(String title, LocalDate productionYear, Genre genre) {
this.title = title;
this.productionYear = productionYear;
this.genre = genre;
}
public Genre getGenre() {
return genre;
}
public void setGenre(Genre genre) {
this.genre = genre;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public LocalDate getProductionYear() {
return productionYear;
}
public void setProductionYear(LocalDate productionYear) {
this.productionYear = productionYear;
}
}
import javax.persistence.*;
@Entity
@Table(name = "genres_table")
public class Genre {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
public Long id;
public String title;
public String description;
public Genre() {
}
public Genre(String title, String description) {
this.title = title;
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
I metoda
@PostMapping
public VideoCasset addCassett(@RequestBody CassetCreator cassetCreator) {
String title = cassetCreator.getTitle();
LocalDate data = LocalDate.parse(cassetCreator.getData());
String genreName = cassetCreator.getGenre();
Genre genre = genreManager.findByTitle(genreName);
VideoCasset videoCasset = new VideoCasset(title, data, genre);
return cassetVideos.save(videoCasset);
}
application.properties
server.port=8084
spring.jpa.properties.hibernate.ddl-auto=update
spring.datasource.url=jdbc:h2:file:./db12
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
Ponieważ post by był zbyt długi jakbym umieścił cały stack trace, najważniejsze punkty to:
2020-03-18 19:40:40.817 WARN 2872 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "drop table genres_table if exists" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "drop table genres_table if exists" via JDBC Statement
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Nie można skasować "GENRES_TABLE" ponieważ zależy od "FK3DREYMM74D126C9ACXU3RVNTW, FK1Q9LQREWC88RNCU68SQMXAGCQ"
Cannot drop "GENRES_TABLE" because "FK3DREYMM74D126C9ACXU3RVNTW, FK1Q9LQREWC88RNCU68SQMXAGCQ" depends on it; SQL statement:
drop table genres_table if exists [90107-200]
2020-03-18 19:40:40.824 WARN 2872 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "create table genres_table (id bigint generated by default as identity, description varchar(255), title varchar(255), primary key (id))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table genres_table (id bigint generated by default as identity, description varchar(255), title varchar(255), primary key (id))" via JDBC Statement
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabela "GENRES_TABLE" już istnieje
Table "GENRES_TABLE" already exists; SQL statement:
create table genres_table (id bigint generated by default as identity, description varchar(255), title varchar(255), primary key (id)) [42101-200]