得到“org.h2.jdbc.JdbcSQLException:表”BOOK_PUBLISHERS“找不到; SQL语句:”

时间:2016-08-01 17:40:08

标签: java spring-boot many-to-many h2

使用Spring Boot,使用Spring Data JPA和H2内存数据库进行映射多对多关系的过程。

我有两张实体书和书。出版商。他们有很多很多关系的地方

创建连接表,将Book_IdPublisher_id作为外键存储在book_publisher表中,如下所示。

create table book_publisher 
(
  book_id number not null,
  publisher_id number not null,
  PRIMARY KEY (book_id, publisher_id),
  CONSTRAINT fk_bookpublisher_book FOREIGN KEY (book_id) REFERENCES CURRENCY (id) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_bookpublisher_publisher FOREIGN KEY (publisher_id) REFERENCES VENUE (id) ON DELETE CASCADE ON UPDATE CASCADE
 ); 

我没有为book_publisher表编写域类。

但我得到了:

  

org.h2.jdbc.JdbcSQLException:找不到表“BOOK_PUBLISHERS”; SQL   声明:插入book_publisher(book_id,publisher_ID)值   (?,?)[42102-191]“在书本对象(书籍类所在的地方)   发布者对象为“SET”变量

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

如果您正在运行您提到的创建脚本,它将在表CURRENCY和VENUE之间创建many_to_many关系。所以我想说你分享的代码片段是错误的,这可能就是为什么没有成功创建表BOOK_PUBLISHER的原因。

创建many_to_many关系的正确SQL代码(在表BOOK和PUBLISHER之间)应该是:

create table book_publisher 
(
  book_id number not null,
  publisher_id number not null,
  PRIMARY KEY (book_id, publisher_id),
  CONSTRAINT fk_bookpublisher_book FOREIGN KEY (book_id) REFERENCES BOOK (id) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_bookpublisher_publisher FOREIGN KEY (publisher_id) REFERENCES PUBLISHER (id) ON DELETE CASCADE ON UPDATE CASCADE
 );